ES6 Primer Series One (Basic)

Source: Internet
Author: User

1. Let command

Tips:

    1. Block-level scope (valid only in the current block)
    2. No variable promotion (must first be declared in use)
    3. Let the variable monopolize the block, no longer subject to external influence
    4. Duplicate declarations are not allowed

In short: Let's more like the variable declaration directives of the static language we know well

ES6 has a new let command to declare variables. The usage is similar to Var, but the declared variable is valid only within the code block where the Let command resides.

Let declares that the variable has only block-level scope

‘use strict‘{  let a = 1;}console.log(a); //结果是什么?

Look at a familiar code:

var a = [];for (var i = 0; i < 10; i++) {  a[i] = function () {    console.log(i);  };}console.log(a[6]()); //结果是什么?

If you use let, what does the following code output?

‘use strict‘var a = [];for (let i = 0; i < 10; i++) {  a[i] = function () {    console.log(i);  };}console.log(a[6]()); // ?

At the same time, when using let, it must be declared and reused, unlike Var's variable elevation:

‘use strict‘console.log(a);let a = 1;

ES6 explicitly stipulates that if the block exists let and const, then the block will form a closed scope, usually in the declaration of apology on the use of these variables, will be error. Abbreviated as "Temporary Dead zone" (Temporal Dead Zone, abbreviated as TDZ).

See a dead zone that's not easy to spot: (Note: The code is not tested)

function bar(x=y, y=2) {  return [x, y];}bar(); // 报错

The error in calling bar is because the parameter x default value equals another parameter y, and y is not declared at this time, which is a dead zone.

It is important to note that the scope of the function parameter scope and the function body are separated:

let foo = ‘outer‘;function bar(func) {  let foo = ‘inner‘;  console.log(func()); // outer}bar(function(){  console.log(foo);});

At the same time, let's not allow duplicate declarations

{  let a = 1;  var a = 1;}{  let a = 1;  let a = 2;}
2. Const command

Tips:

    1. Const is used to declare constants, and once declared, values cannot be changed
    2. Const has block-level scope
    3. Const cannot be variable promoted (declared after use)
    4. Non-repeating declaration

Const looks like a read-only object of a static language we know well

Const declares a constant, and once declared, the value will be immutable.

‘use strict‘const PI = 3.1415;PI // 3.1415PI = 3; //Error

The const directive points to the address where the variable is located, so it is possible to set the property of the variable (without changing the address of the variable), and you can use the freeze if you want to be completely immutable (including attributes).

‘use strict‘const C1 = {};C1.a = 1;console.log(C1.a); // 1 //冻结对象,此时前面用不用const都是一个效果const C2 = Object.freeze({}); C2.a = 1; //Error,对象不可扩展console.log(C2.a);
3. Global Object Properties

JavaScript, the global object is the topmost object, the browser is the Window object, node is the global object, ES5, all global variables are the properties of the global object.

In ES6, var and function declaration variables, which are attributes of global objects, let and const are not properties of global objects.

‘use strict‘let b = 2;console.log(global.b); // undefined

ES6 Starter Series One (basic)

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.