var fName = 'Peter', sName = 'Smith', age = 43, job = 'photographer'; var a = 'Hi, I\'m ' + fName + ' ' + sName + ', I\'m ' + age + ' and work as a ' + job + '.'; var b = `Hi, I'm ${ fName }${ sName }, I'm ${ age } and work as a ${ job }.`;
let a = [3, 4, 5]; let b = [1, 2, ...a, 6]; console.log(b); // [1, 2, 3, 4, 5, 6] This can be very useful for passing in a set of variables to a functionfrom an array.
functionfoo(a, b, c) { console.log(`a=${a}, b=${b}, c=${c}`)} let data = [5, 15, 2]; foo( ...data); // a=5, b=15, c=2
functionbaz() { return { x: 'car', y: 'London', z: { name: 'John', age: 21} }; } let { x: vehicle, y: city, z: { name: driver } } = baz(); console.log( `I'm going to ${city} with ${driver} in their ${vehicle}.` ); // I'm going to London with John in their car.
对象解构允许的另一件事是为多个变量赋值。
1 2
let { x: first, x: second } = { x: 4 }; console.log( first, second ); // 4, 4
对象字面量和简明参数
当您从变量创建对象字面量时,ES6 允许您在与 key 与变量名称相同的情况下省略 key 名。
1 2 3 4
let a = 4, b = 7; let c = { a: a, b: b }; let concise = { a, b }; console.log(c, concise) // {a: 4, b: 7}, {a: 4, b: 7}
// pre ES6 let a = foo(), name = a.name, age = a.age, company = a.job.company; // ES6 destructuring and concise parameters let { name, age, job: {company}} = foo();
let person = { name: 'Anna', age: 56, job: { company: 'Tesco', title: 'Manager' } }; // method 1 functionold1(person) { var yearOfBirth = 2018 - person.age; console.log( `${ person.name } works at ${ person.job.company } and was born in ${ yearOfBirth }.`); } // method 2 functionold1(person) { var age = person.age, yearOfBirth = 2018 - age, name = person.name, company = person.job.company; console.log( `${ name } works at ${ company } and was born in ${ yearOfBirth }.`); } // method 3 functiones6({ age, name, job: {company}}) { var yearOfBirth = 2018 - age; console.log( `${ name } works at ${ company } and was born in ${ yearOfBirth }.`); }
使用 ES6,我们可以提取age,name和company而无需额外的变量声明。
动态属性名称
ES6 添加了使用动态分配的 key 创建或添加属性的功能。
1 2 3 4 5 6
let city= 'sheffield_'; let a = { [ city + 'population' ]: 350000 }; a[ city + 'county' ] = 'South Yorkshire'; console.log(a); // {sheffield_population: 350000, sheffield_county: 'South Yorkshire' }
箭头函数
箭头函数有两个主要方面:结构和this绑定。
它们可以具有比传统功能更简单的结构,因为它们不需要功能关键字,并且它们自动返回箭头之后的任何内容。
1 2 3 4
var foo = function( a, b ) { return a * b; } letbar = ( a, b ) => a * b;
如果函数需要的不仅仅是一个简单的计算,可以使用大括号,并且该函数会返回花括号块范围返回的任何内容。
1 2 3 4 5
letbaz = ( c, d ) => { let length = c.length + d.toString().length; let e = c.join(', '); return`${e} and there is a total length of ${length}`; }
对于箭头函数最有用的地方之一是在map(),forEach()或sort()之类的数组函数中。
1 2 3
let arr = [ 5, 6, 7, 8, 'a' ]; let b = arr.map( item => item + 3 ); console.log(b); // [ 8, 9, 10, 11, 'a3' ]
ES6 增加了一种迭代数组中每个值的方法。 这与现有的 ... in 不同,它循环使用key/index。
1 2 3 4 5 6 7 8 9
let a = ['a', 'b', 'c', 'd' ]; // ES6 for ( var val of a ) { console.log( val ); } // "a" "b" "c" "d" // pre-ES6 for ( var idx in a ) { console.log( idx ); } // 0 1 2 3
ES6添加了一种新格式,在最初的 0 之后添加一个 o (注意是字母)以将该数字声明为八进制数。ES6 还添加了二进制格式。
1 2 3 4 5
Number( 29 ) // 29 Number( 035 ) // 35 in old octal form. Number( 0o35 ) // 29 in new octal form Number( 0x1d ) // 29 in hexadecimal Number( 0b11101 ) // 29 in binary form