this represents the object calling the script.
Take for example:
var x = 35;
var places x here, right within this context.
And what is this context? This is the
And what is the global context? it is the object
So now we can call back x in the console:
and the output is 35, right?
We could also do it this way:
and the output is 35.
So x; is actually a
shortcut for window.x;
But the question is: What is
this is a placeholder. It is meant to be generic. It will be replaced by the object that owns the environment.
Who owns the current environment? window
So instead of
We could code it
And why would we do such thing?
Because once we create a script using
this instead of the object name itself, it becomes
this.x; we can take this script anywhere and it will always work under that specific environment where it is being applied.
Easier said than done! There are some issue but they are beyond this explanation.
You can read more about those issues on my latest book.
Use the free amazon “Kindle for PC” app to read it on your computer.