It assumes that in the closure scope (If none it will be in the global scope such as window
) there is a method called methodA. Then again, because the this is really the enclosing scope mentioned), it will remind it and assign it ti be used be through object a as well.
So you can execute it by:methodA()
or a.methodA()
Edit to explain closure:
Although Javascript is very similar to Java/C++ in syntax it's quite different than both of them in the sense that when a function is instantiated as an object, it remembers the scope it was instantiated in. I would highly recommend anyone doing more than the casual JS(if there is such a thing), to look into this article.
The this
in JS will deffer depends if it was created inside an instantiated function - AKA: new MyClass()
. Referring to a literal object such as:
var myObj={a:this.b}
will not create a new this
and will by default refer to enclosing scope. If none was created it will be the global object such as window
in a browser