对象常量/初始化程序中的自引用 - Self-references in object literals / initializers

问题:

Is there any way to get something like the following to work in JavaScript? 有什么办法能够使如下内容在JavaScript中起做用? this

var foo = {
    a: 5,
    b: 6,
    c: this.a + this.b  // Doesn't work
};

In the current form, this code obviously throws a reference error since this doesn't refer to foo . 在当前形式中,该代码明显抛出一个参考错误,由于this不是指foo But is there any way to have values in an object literal's properties depend on other properties declared earlier? 可是, 是否有任何方法能够使对象文字的属性值取决于先前声明的其余属性? spa


解决方案:

参考一: https://stackoom.com/question/JMss/对象常量-初始化程序中的自引用
参考二: https://oldbug.net/q/JMss/Self-references-in-object-literals-initializers
相关文章
相关标签/搜索