简体   繁体   English

为什么“ this”不能与console.log一起使用?

[英]Why isnt “this” work with console.log as an argument?

I assumed that .call should //change the context of this in the log method to obj, but it doesnt seem to be //doing that, it seems to refer to the window object as if I used this side a function 我以为.call应该//将log方法中的上下文更改为obj,但是似乎并没有这样做,似乎是在引用window对象,就像我在这边使用了一个函数一样。

let obj={
    a: "this should work right?"
}

console.log.call(obj,this.a);//returns undefined

you are not in a function scope, instead you should test this way: 您不在函数范围内,而是应该这样测试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM