【发布时间】:2017-08-05 20:49:30
【问题描述】:
看到下面的ES6代码,一头雾水:
class GuitarAmp {
constructor ({ cabinet = 'spruce', distortion = '1', volume = '0' } = {}) {
Object.assign(this, {
cabinet, distortion, volume
});
}
}
Object.assign 的第二个参数是什么?它不是一个对象,那它是什么?我刚刚注意到它也是构造函数参数的一部分,这部分:
{ cabinet = 'spruce', distortion = '1', volume = '0' } = {}
我不熟悉这种新语法,所以我不知道如何查找它,因为我不知道它叫什么。有人知道这个词吗?
【问题讨论】:
-
在上面的链接中搜索“Shorthand property names (ES2015)”——基本上等于
Object.assign(this, { cabinet: cabinet, distortion: distortion, volume: volume }); -
@Aerovistae 不是三个参数,是一个参数对象被解构了。