0

I am new to js and not able to understand which this does not points to the "obj" in the below code: var obj = { x:this }; obj.x; // window object

I have referred many sites but all states the this always points to the object in whose context the method was called..does that not hold true for property??

Also why this pointing becomes correct inside a function as property of same object?? I have read that scope and context are not to be confused are different, then why only function is creating right context??I read that scope are of two type local and global then why context is affected.

Please explain in details, this question might be irrelevant maybe because i am still confused in scope and context..shares useful links too.

  • There's no method call in your code. Yes, locating code inside a property expression doesn't change anything. – Bergi Apr 14 '18 at 18:23
  • so scope and context only be changed by function?? – a.developer Apr 14 '18 at 18:25
  • Scopes (that introduce variables) also exist for blocks. Yes, `this` is strongly related to functions, where is some kind of implicit parameter that is set by each call. – Bergi Apr 14 '18 at 18:28

0 Answers0