Javascript 你能解释一下这个物体在不同情况下的属性吗 让我考虑一个对象“MSG”,我在下面的例子中重新定义它,但是我不理解它的属性(特别是M1)的行为。为什么它在对象内部是未定义的,当我通过函数访问它时,它被赋予字符串值(最后一种情况)。你能解释一下每种情况吗 case 0 var msg = { m1 : "this is string", m2 : "ok, " + this.m1 }; console.log(msg.m1); //this is string console.log(msg.m2); // ok, undefined /////// why m1 is undefined inside msg //------------------------------------------ case 1 var msg1 = new Object(msg); console.log(msg1.m1); //this is string console.log(msg1.m2); // ok, undefined //again undefined //------------------------------------------ case 2 var msg2 = { m1 : function () { return "this is string";}, m2 : "ok, " + this.m1, m3 : typeof this.m1 }; console.log(msg2.m1()); //this is string console.log(msg2.m2); // ok, undefined console.log(msg2.m3); // undefined console.log(typeof msg2.m1) // function 'but inside msg2 it is undefined why' //------------------------------------------ case 3 var msg3 = { m1 : (function () { return "this is string";}()), m2 : "ok, " + this.m1, m3 : typeof this.m1 }; console.log(msg3.m1); //this is string console.log(msg3.m2); // ok, undefined console.log(msg3.m3); // undefined console.log(typeof msg3.m1) // string (atleast i know why this is ) but inside msg2 it is not defined (why ) //------------------------------------------ case 4 var msg4 = { m1 : (function () { return "this is string";}()), m2 : function () { return "ok, " + this.m1; }, m3 : typeof this.m1 }; console.log(msg4.m1); //this is string console.log(msg4.m2()); // ok, this is string console.log(msg4.m3); // undefined console.log(typeof msg4.m1) // string (atleast i know why this is ) but inside msg2 it is not defined and in m2 it evaluated (why so)

Javascript 你能解释一下这个物体在不同情况下的属性吗 让我考虑一个对象“MSG”,我在下面的例子中重新定义它,但是我不理解它的属性(特别是M1)的行为。为什么它在对象内部是未定义的,当我通过函数访问它时,它被赋予字符串值(最后一种情况)。你能解释一下每种情况吗 case 0 var msg = { m1 : "this is string", m2 : "ok, " + this.m1 }; console.log(msg.m1); //this is string console.log(msg.m2); // ok, undefined /////// why m1 is undefined inside msg //------------------------------------------ case 1 var msg1 = new Object(msg); console.log(msg1.m1); //this is string console.log(msg1.m2); // ok, undefined //again undefined //------------------------------------------ case 2 var msg2 = { m1 : function () { return "this is string";}, m2 : "ok, " + this.m1, m3 : typeof this.m1 }; console.log(msg2.m1()); //this is string console.log(msg2.m2); // ok, undefined console.log(msg2.m3); // undefined console.log(typeof msg2.m1) // function 'but inside msg2 it is undefined why' //------------------------------------------ case 3 var msg3 = { m1 : (function () { return "this is string";}()), m2 : "ok, " + this.m1, m3 : typeof this.m1 }; console.log(msg3.m1); //this is string console.log(msg3.m2); // ok, undefined console.log(msg3.m3); // undefined console.log(typeof msg3.m1) // string (atleast i know why this is ) but inside msg2 it is not defined (why ) //------------------------------------------ case 4 var msg4 = { m1 : (function () { return "this is string";}()), m2 : function () { return "ok, " + this.m1; }, m3 : typeof this.m1 }; console.log(msg4.m1); //this is string console.log(msg4.m2()); // ok, this is string console.log(msg4.m3); // undefined console.log(typeof msg4.m1) // string (atleast i know why this is ) but inside msg2 it is not defined and in m2 it evaluated (why so),javascript,function,object,this,Javascript,Function,Object,This,此永远不会引用当前通过对象文字语法生成的对象 此引用环境的调用上下文。对象本身没有调用上下文,但它们可以用作调用上下文 请注意您的msg4.m2()工作正常。这是因为this是函数的调用上下文,是对调用方法的对象的引用,即msg4对象 msg4是如何成为m2函数的调用上下文的?因为当你这样做的时候: msg4.m2(); 您正在调用msg4对象上的m2。这会自动设置调用上下文,以便This指向msg4对象。如果尝试m2:“ok,”+=This.m1@RASG:ReferenceError,会发

永远不会引用当前通过对象文字语法生成的对象

引用环境的调用上下文。对象本身没有调用上下文,但它们可以用作调用上下文

请注意您的
msg4.m2()
工作正常。这是因为
this
是函数的调用上下文,是对调用方法的对象的引用,即
msg4
对象

msg4
是如何成为
m2
函数的调用上下文的?因为当你这样做的时候:

msg4.m2();

您正在调用
msg4
对象上的
m2
。这会自动设置调用上下文,以便
This
指向
msg4
对象。

如果尝试
m2:“ok,”+=This.m1
@RASG:ReferenceError,会发生什么<代码>+=在字符串文字上?那是行不通的。
+=
的左边必须是左值。好吧,你的意思是我不能使用object的一个属性来计算另一个属性,比如
container:$(“#container”)
那么就没有直接的方法来为
imgs:container.find('img')
使用
container(有什么简单的方法可以理解这个概念吗)@NatwarSingh:在构建对象时不需要。您首先需要引用现有对象,无论是通过变量还是通过
this
。您更新了注释。请稍候…@NatwarSingh:使用
容器
示例,您首先需要定义对象。您可以在您可以定义它。但要在设置
imgs
属性时引用该对象,您需要在创建该对象后执行该操作,如
myobject.imgs=container.find(“img”)
@NatwarSingh:只有函数才有调用上下文。这意味着
this
的值只在函数内部更改。当您使用对象文字语法定义对象时,它不会更改。这就是为什么
{foo:“foo”,bar:this.foo}
不起作用。
this
将是对函数的引用(或全局)上下文是,但当您使用该语法时,它永远不会是对对象的引用。…
非常动态。它可以通过多种不同的方式设置,但仅在调用函数时设置(全局除外)。它绑定到函数调用,而不是任何其他结构。例如,
this
if
语句中永远不会更改。
alert(this);if(true){alert(this);}
这两个警报将具有相同的
this
值。