
JavaScript
新特性
|
25
sound,
powderYell() {
let yell = this.sound.toUpperCase()
console.log(`${yell} ${yell} ${yell}!!!`)
},
speed(mph) {
this.speed = mph
console.log('speed:', mph)
}
}
对象语义增强允许我们将全局变量变成对象,并且因为省去了不必要的关键字
function
,从而减少了代码的输入工作。
扩展运算符
扩展运算符是通过三个点号来完成多种工作任务的。首先,扩展运算符允许用户联合
数组中的内容。比如我们有两个数组,可以通过将二者合二为一的方式构造第三个数
组:
var peaks = ["Tallac", "Ralston", "Rose"]
var canyons = ["Ward", "Blackwood"]
var tahoe = [...peaks, ...canyons]
console.log(tahoe.join(', ')) // Tallac, Ralston, Rose, Ward, Blackwood
数组
peaks
和
canyons
中的所有元素都被移动到名为
tahoe
的新数组中了。
让我们来看看扩展运算符是如何帮助用户解决问题的。还是以上面示例中的
peaks
数
组为例,假设我们希望获取该数组中的最后一个元素。可以使用
Array.reverse