Javascript 为什么不是;这";是否将console.log用作参数?

Javascript 为什么不是;这";是否将console.log用作参数?,javascript,this,Javascript,This,我假设.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 myFun

我假设.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);

函数的参数在调用函数之前进行求值,因此上下文不会在此时设置为
obj
。因为
this中的
this
。a
不会因使用而更改。call-Barmar说得更好:p这是执行上下文的属性,不应称为“上下文”.可能是@Z.Bagley的副本-我认为这不是副本。OP遗漏了
this。在将
传递到console.log之前,将对其进行评估,因此方法中引用的任何内容都与整个语句的结果无关。