【问题标题】:can you please explain the behaviour of property of this object in these different cases你能解释一下在这些不同情况下这个对象的属性的行为吗
【发布时间】:2012-09-19 14:00:55
【问题描述】:

假设我有一个对象“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) 

【问题讨论】:

  • 如果你尝试m2 : "ok, " += this.m1会发生什么
  • @RASG:参考错误。 += 在字符串文字上?那是行不通的。 += 的左侧必须是左值。

标签: javascript function object this


【解决方案1】:

this 永远不会引用当前通过对象字面量语法生成的对象。

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

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

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

msg4.m2();

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

【讨论】:

  • 好吧你的意思是我不能使用对象的一个​​属性来评估另一个像container:$('#container') 然后没有直接的方法使用containerimgs: container.find('img') 我真的不明白第二部分(是有什么简单的方法可以理解这个概念)
  • @NatwarSingh:不是在构建对象时。您首先需要一个对现有对象的引用,无论是通过变量还是通过this。您更新了您的评论。等一下……
  • @NatwarSingh:在您的container 示例中,您首先需要定义对象。您可以在定义 container 属性时添加它。但是要在设置imgs 属性时引用该对象,您需要在创建对象后进行,例如myobject.imgs = container.find("img")
  • @NatwarSingh:只有函数有调用上下文。这意味着this 的值仅在函数内部发生变化。当您使用对象文字语法定义对象时,它不会改变。这就是{foo: "foo", bar: this.foo} 不起作用的原因。 this 将是对函数 (或全局) 上下文的任何引用,但当您使用该语法时,它永远不会是对对象的引用。
  • ...this 非常动态。它可以通过多种不同的方式设置,但它在调用函数(全局除外)时设置。它绑定到函数调用,而不是任何其他结构。例如,this 永远不会在 if 语句中更改。 alert(this); if (true) { alert(this); } 这两个警报将具有相同的 this 值。
猜你喜欢
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多