[英]Why isnt “this” work with console.log as an argument?
我以为.call应该//将log方法中的上下文更改为obj,但是似乎并没有这样做,似乎是在引用window对象,就像我在这边使用了一个函数一样。
let obj={
a: "this should work right?"
}
console.log.call(obj,this.a);//returns undefined
您不在函数范围内,而是应该这样测试:
let obj={
a: "this should work right?"
};
let myFunc = function() { // u define a function scope here
return this.a;
};
console.log(myFunc.call(obj));
// console.log.call(obj,this.a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.