Why is ES6
Every birth criteria are meant to strengthen the language of sound, function, JavaScript language itself, there are some places unsatisfactory
- Variable upgrade feature adds unpredictability program is running
- The syntax is too loose, to achieve the same functionality, different people might write different code
ES6 new grammar
let
ES6 the new keyword is used to declare variables
- Variables declared valid only let located block level
- To prevent the loop variable becomes a global variable
- There is no variable lift
- Block-level temporary area where the dead and let bind
NOTE: Use only let keyword variable declared with block-level scope, a variable declared with var block-level scope does not have the characteristic
const
Role: declare a constant, constant values can not be changed is the value (memory address)
- Having a block-level scope
- When the assignment must declare constants
- When constant values, the value can not be changed (can not change the memory address)
let const var difference
- var scoped within the function where the play of the statement, and there is a variable phenomenon upgrade
- let the scope of the statements of the block where the variable lift absent
- const appears later in the code can not modify the value of this constant
Deconstruction assignment
- S6, allows the extraction of values from the array, in a corresponding position, variable assignment, the object can be achieved deconstructed
- According to a certain pattern, or an array of values from the extracted object, the value assigned to the extracted additional variables
An array of deconstruction
- Array structure allows us to-one relationship extract value from the array and the value assigned to the variable
One correspondence, if no value is undefind
Object deconstruction
Arrow function
() => {}
To simplify the function definition syntax
- Only the results of a function in the code, and the code is returned, one can omit the braces
- If the function parameter is only one parameter is outside the parentheses may be omitted
function fn(v) {
return v;
}
const fn = v => v;
this points to a problem
Arrow function does not bind this keyword, the function of this arrow, points to the context of this function defines the location
The remaining parameters
The remaining parameter syntax allows us to a variable number of arguments is represented as an array
const sum = (...args) => {
let total = 0;
args.forEach(item => total += item)
return total;
}
console.log(sum(10,20)); //30
console.log(sum(10,20,30)); //60
The remaining parameters and with the use of deconstruction
let ary = ['zhangsan','lisi','wangwu'];
let [s1,...s2] = ary;
console.log(s1); //'zhangsan'
console,log(s2);//'zhangsan', 'lisi'