为什么“this”不能用console.log作为参数?

时间:2017-03-20 02:56:34

标签: javascript this

我认为.call应该//将log方法中的this的上下文更改为obj,但它似乎并没有这样做,它似乎引用了window对象,好像我在这边使用了一个函数

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

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

1 个答案:

答案 0 :(得分:0)

您不在功能范围内,而应该以这种方式进行测试:

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);