我正在尝试打印下面的 Javascript 代码,它在第一行显示 true
,在第二行显示 false
。尽管它们看起来一样,但我不明白它们的区别
var someObj = {
data: function() {
console.log(this == someObj) // true
console.log("Value of this " + this == someObj) //false
}
};
someObj.data();
最佳答案
+
将字符串连接到对象的 toString,以便您进行比较:
("Value of this " + this) == someObj
而不是
"Value of this " + (this == someObj)
var someObj = {
data: function() {
console.log(this == someObj) // true
console.log("Value of this " + this == someObj) // false
console.log("Value of this " + (this == someObj)) // true
}
};
someObj.data();
关于javascript - 理解 Javascript 中的 this 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353289/