When I tell that "It's simpler to think of this as the reference to the object that is executing that block of code", I'm trying to give an intuitive definition of the behavior of this when is implicitly passed.
In your example above, you are explicitly passing x as the reference for this. So, the definition I gave doesn't apply to this case.
If you didn't indicate what object this should refer, it will reference the window object:
Using the definition I gave, you could think of this behavior as: the window object is executing the function foo(), so this will refer to window.
You are right, this is a parameter that you can use to access the context object. I thought it will be easier for beginners to use a non-technical definition when this is implicitly passed.
Thanks for exposing your thoughts on the article!
When I tell that "It's simpler to think of
this
as the reference to the object that is executing that block of code", I'm trying to give an intuitive definition of the behavior ofthis
when is implicitly passed.In your example above, you are explicitly passing
x
as the reference forthis
. So, the definition I gave doesn't apply to this case.If you didn't indicate what object
this
should refer, it will reference the window object:Using the definition I gave, you could think of this behavior as: the
window
object is executing the functionfoo()
, sothis
will refer towindow
.You are right,
this
is a parameter that you can use to access the context object. I thought it will be easier for beginners to use a non-technical definition whenthis
is implicitly passed.What I like about starting with the foo.call(x) example is that it immediately drains all of the magic.
There are no methods in javascript, functions do not belong to classes.
There are just functions which are invoked with some object as their 'this' context.
Having understood this, you can then move on to a.b(c) and show that it's equivalent to a.b.call(a, c).
I had not thought of it that way, I appreciate your reflection on the subject and I will consider it for future articles.
Thank you! :D
You are welcome. :)