我想我可能遗漏了一些关于对象引用的东西,在下面的例子中,this是否引用了测试对象?如果没有,我怎么能在最后的test.a = test.b中声明b呢?
test = {
a: 1,
b: this.a,
check : function(){
console.log(test.a); // returns 1
console.log(test.b); // returns undefined
}
};
test.check();非常感谢
发布于 2012-11-21 23:14:11
您可以这样声明它:
function test(){
this.a = 1;
this.b = this.a;
this.check = function(){
console.log(this.a); // output 1
console.log(this.b); // output 1
}
}
var t = new test();
t.check();现场示例:http://jsfiddle.net/Rqs86/
发布于 2012-11-21 23:09:23
在声明对象时,test.b指的是this.a。
var foo = this;你不会期望this在这里提到foo,对吧?它在这里的工作原理完全相同:
var bar = [ this ];和
var baz = { 'blag' : this };https://stackoverflow.com/questions/13496088
复制相似问题