2

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

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

console.log.call(obj,this.a);//returns undefined
Samatha
  • 45
  • 5
  • Arguments to a function are evaluated before calling the function, so the context isn't set to the `obj` at that point. – Barmar Mar 20 '17 at 02:58
  • because `this` in `this.a` isn't changed by using .call - Barmar said it better :p – Jaromanda X Mar 20 '17 at 02:59
  • *this* is a property of an execution context, it shouldn't be referred to as "context". – RobG Mar 20 '17 at 03:00
  • Possible duplicate of [console.log object at current state](http://stackoverflow.com/questions/7389069/console-log-object-at-current-state) – Z. Bagley Mar 20 '17 at 03:02
  • @Z.Bagley—I don't think that's a duplicate. The OP has missed that `this.a` is evaluated before it's passed to *console.log*, so whatever *this* refers to in the method is irrelevant to the result of the overall statement. – RobG Mar 20 '17 at 03:12

1 Answers1

0

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);
Xin
  • 33,823
  • 14
  • 84
  • 85