有没有办法在JavaScript中使用以下内容?
var foo = {
a: 5,
b: 6,
c: this.a + this.b // Doesn't work
};
Run Code Online (Sandbox Code Playgroud)
在当前形式中,此代码显然会抛出引用错误,因为this没有引用foo.但是,是有什么办法对早些时候宣布的其他属性在对象文本的属性值依赖?
我只是想知道以下JavaScript对象声明之间的区别.具体来说,事物类中的事物对象文字和事物1对象之间的区别.
var thing = {
sanity:0,
init:function(){
//code
},
send:function(){
//code
}
}
function thing(){
this.sanity = 0;
this.init = function(){
//code
};
this.send = function(){
//code
};
}
thing1 = new thing();
Run Code Online (Sandbox Code Playgroud)