Introduction of ES6
ES6, short for ECMAScript 6.0, is the next generation standard for the JavaScript language, which was officially released in June 2015. The goal is to unify JavaScript syntax standards that can be used to develop large applications, known as enterprise development languages.
Relationship between ES6 and JavaScript: ES6 is the specification standard for JavaScript, and JavaScript is an implementation of ES6.
Variable/assignment
In ES5, the scopes are: global scopes, function scopes, and no block scopes. ES6 adds block-level scopes. Block scopes are included by {}, and {} in an if statement is also a block-level scope.
Var a = 12; var a = 12; var a = 12; console.log("a",a) } console.log("a",a)
Function (){var b = 5; var b = 5; })() console.log("b",b) // not defined
Var is different from let and const:
type | define | Variable types | scope |
---|---|---|---|
var | You can redefine it | Is a variable and cannot be restricted | There is no block-level scope |
let | You can’t repeat the definition | Is a variable | Block – level scope that cannot be accessed across blocks |
const | You can’t repeat the definition | Is a constant and must be assigned during initialization | Block – level scope that cannot be accessed across blocks |
Can the properties of objects defined by const be changed?
Example 1: Modify the property value of the object.
Const per = {name:' Qian Qian '} per. Name = "I am Hip Hop" console.log(" Per.name ",per. Name) // Print out I am Hip Hop
In the last example, we found that the properties of objects can be modified. Why?
The object is a reference type. Per stores only the pointer of the object, which means that the pointer will not change. Modifying the properties of the object will not change the pointer of the object, so it is allowed to change.
Assignment: Add a destructed assignment, which takes the data apart and assigns it. Two rules for destructing assignment:
- The left and right patterns must be consistent
- Definitions and assignments must be done simultaneously.
Example 2: Correct structure assignment
Let [a, b, c] = [1, 2, 3]. The console log (" a ", a) / / 1 console. The log (" b ", b) / / 2. The console log (" c ", c) / / 3
Example 3: The left and right sides have the same pattern and different data lengths
let [bar, foo] = [1];
console.log("bar",bar)//1
console.log("foo",foo) // undefined
The above deconstruction failed. The variable value is equal to undefined.
Note: Objects can also be deconstructed, but it is important to note that object and array deconstructions are very different. Object properties are not ordered, and variables must have the same name as properties in order to get the correct value.
Example 4: Deconstructive assignment of an object
let { bar,foo } = {
foo:'aaa',
bar:'bbb'
}
console.log('bar',bar)
console.log('foo',foo)
console.log('baz',baz) // not defined