ES6 must know (1) - variable declaration and structure assignment

This article belongs to a personal summary of some of the more commonly used grammars of es6. It mainly refers to Ruan Yifeng <a href="http://es6.ruanyifeng.com//">ECMAScript6 Introduction</a>, if any Please criticize and correct me if I misunderstand or not understand clearly~

ps: It is recommended to use es6 online debugging

<b>let 和 const</b>

1. The variables declared by let are only valid in the current code block, and do not have variable promotion (that is, if variables declared by let in the code block are used in advance, an error will be reported);

{
   console.log(a)    // a is not defined
   console.log(b)   //  2
   let a = 1; var b = 2; } 

2.Let duplicate declarations are not allowed in the same scope;

3.const declares a read-only constant, once declared, it cannot be changed;

4. When const declares a variable, it must be initialized, and the variable can only be valid in the current scope;

5. When const declares a type of data (mainly objects and arrays), it saves the memory address of the variable, which can only guarantee that the address is fixed, but cannot guarantee that the data structure remains unchanged;

const o = {};
o.name = 'hello';    //可以给对象添加属性
console.log(o);      // { "name" : "hello" } o = {}; //报错,因为o的内存地址不能改变 

6. You can use the Object.freeze() method to freeze an object or a property of an object;

7. ES5 has two ways to declare variables: var and function; ES6 has six ways to declare variables: var let const function import class;

<b>Destructuring assignment of variables</b>

1. ES6 allows to extract values ​​from arrays and objects and assign values ​​to variables according to a certain pattern;

2. If the variable destructuring is unsuccessful, it will return undefined;

3. As long as a certain data structure has an Iterator interface, destructuring assignment in the form of an array can be used;

4. You can specify default values ​​when destructuring assignments ( let [ foo = true ] = [] );

5. If a member of the array is not strictly equal to ( === ) undefined , the default value will not take effect;

let [x, y = 'b'] = ['a']; // x='a', y='b' let [x, y = 'b'] = ['a', 'undefined']; // x = 'a' , y = 'undefined' 

6. If the specified default value is an expression, then the expression is lazy evaluated (that is, it is only evaluated when it is used);

7. The default value can refer to other variables assigned by destructuring, but the variable must have been declared;

8. In the deconstruction of the object, the variable must have the same name as the property in order to get the correct value. If the variable does not have a corresponding property with the same name, the value will not be obtained, and finally it is equal to undefined;

9. The destructuring assignment of an object is to first find the property with the same name, and then assign it to the corresponding variable. It is the latter that is really assigned, not the former;

let { foo: baz , bar } = { foo: "aaa", bar: "bbb" }; foo // foo is not defined baz // 'aaa' bar // 'bbb' 

In the above example foo is the matched pattern and baz is the variable. The variable baz is actually assigned, not the pattern foo. The reason bar matches is because the destructuring assignment of an object is a shorthand of the form

let { foo: foo, bar: bar } = { foo: "aaa", bar: "bbb" }

10. The destructuring assignment of variables is usually used for exchanging variables, function parameter definitions, parameter specifying default values, etc. Although destructuring assignment is very convenient, it is not easy to parse. If it is used improperly, it will cause problems that are different from the expected values~




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326031695&siteId=291194637