我看到了以下ES6代码,并感到困惑:
class GuitarAmp {
constructor ({ cabinet = 'spruce', distortion = '1', volume = '0' } = {}) {
Object.assign(this, {
cabinet, distortion, volume
});
}
}Object.assign的第二个参数是什么?它不是一个物体,那它是什么?我刚刚注意到它也是构造函数参数的一部分,这部分:
{ cabinet = 'spruce', distortion = '1', volume = '0' } = {}我不熟悉这个新语法,所以我不知道如何查找它,因为我不知道它叫什么。有人知道这个词吗?
发布于 2017-03-15 08:19:28
因此,在上述守则中,我相信:
{
cabinet, distortion, volume
}在ES5中:
{
cabinet: cabinet,
distortion: distortion,
volume: volume,
}当键和值相同时,它只是编写对象的一种简短形式。
发布于 2017-03-15 08:23:11
第一个param的默认值是{},但如果提供它,则在第一个param对象(如cabinet = 'spruce', distortion = '1', volume = '0' )中有嵌套的默认值。
因此,如果提供了第一个对象param,例如{ cabinet = 'test', distortion = '4', volume = '2' },那么this将如下所示
{
cabinet: 'test',
distortion: '4',
volume: '2'
}否则,它将在param中提供默认值。
发布于 2017-03-15 08:24:22
它是一个使用速记语法的对象文字。只是句法上的糖。
var a = "A";
var b = "B";
var c = "C";这两者是相同的:
{ a: a, b: b, c: c }
{ a, b, c }换句话说,如果您向对象文本中抛出一个变量,则属性名将成为变量名的名称。
https://stackoverflow.com/questions/42804267
复制相似问题