Enter ES6 (1)---first knowledge es6

Source: Internet
Author: User
Tags babeljs

ES6, full name ECMAScript6 (aka es2015). What is ECMAScript? What do we often say about JavaScript and how it's connected?

Nanyi Teacher has a sentence to describe the more accurate: "ECMAScript is the international standard of JavaScript language, JavaScript is the implementation of ECMAScript." JS has several large versions, now used more is the ES3,ES5. And last year, ES6 's standards were introduced, and for every front-end developer, embracing the new standards was inevitable, so from now on, ES6 should be the basic skill of a front-end developer. OK, the question about the standard stops here, now let's see how to use the ES6!

  Everything starts hard.

After all, it is also a new standard, so there are still a lot of problems with browser support. However, with the promotion of standards, I believe that the browser will be more and more support. From here you can see how much support your current browser has for ES6.

However, there are many ways for you to write and run the ES6 program, listed below are two ways for everyone to refer to:

  1. Use the Babel transcoding device in the command line.

Two tools to install first

NPM Install Babel-cli-g (Babel command-line tool)

NPM Install babel-perset-es2015

Refer to http://babeljs.io/docs/plugins/preset-es2015/for specific use

This method is relatively fast and can be used as a primer practiced hand.

  2. Using the Babel online encoder

    You can write your ES6 code in http://babeljs.io/repl/and it will automatically turn you into ES5 code.

Highly recommended!

Of course there are other ways, and the students who are very skilled with node can also try to write ES6 code in node.

  Variable Promotion issues

  

This is the first question that I took part in Ali's spring recruit last year. This topic uses very simple code to describe a problem that many people are not aware of---variable elevation.

I get this question when a bit of a crazy, so directly to the browser to run the addition of a option, is it strange?

The so-called variable promotion, refers to the same function scope, the variable is called before the declaration, this will return undefined. It is true that this variable promotion is a big design error compared to c,c++, so the standard in es6 discards the problem. A let command is used to declare.

Console.log (test); Error

Let test = 10;

  Let&const Advent

Let,const lets JS move closer to the traditional object-oriented language. Why do you say that?

In addition to the variable elevation issue just mentioned, there are several problems with variable declarations, such as repeating declarations, scopes, closures.

Duplicate declaration: In Es3 and ES5, there is no problem with continuous var two variables, but it is often ridiculous. If such a grammar is present in the ES6, it cannot be passed.

Scope: In Es3 and ES5, functions outside the function can access variables in the block-level scope (such as if,for), so in the JavaScript Advanced programming book, the author mentions that JS does not have block-level scope. In Es6, the block-level scope gives JS more guaranteed vitality, and the problem of global variable leakage is solved.

Closures: This is a relatively difficult concept, in Es3 and ES5, the use of closures to solve the problem between many function scopes, in ES6 can use let to replace the closure. As the closure concept is more important, temporarily put in the future blog post detailed explanation.

In order to be able to specify the usage of let,const, you can go here to see the actions associated with them: Let&const's Little Demo

  

Let and const are just the simplest features, so begin to embrace the standard slowly and walk into the ES6.

  

  

    

    

Enter ES6 (1)---first knowledge es6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.