A question spurred by curiosity, if I have the following code, what's the benefit (beyond just the simplicity) of calling a property via this
instead of user
in the show_name
method?
var user = {
name : 'John Doe',
show_name : function(){
alert(this.name);
// OR
alert(user.name);
}
};
In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object. Alone, this refers to the global object.
“This” keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object.
The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document. getElementById("id_of_element").
The difference becomes obvious, if you have a look at this example. It creates a second object and sets the prototype accordingly.
var user = {
name : 'John Doe',
show_name : function(){
alert(this.name);
// OR
alert(user.name);
}
};
user2 = Object.create( user );
user2.name = "someone else";
user2.show_name();
Here this.name
refers to the current object's name
property, whereas user.name
always refers to the original name
property.
By using this
you ensure that after changing your variable name e.g. from user
to something
your code still works.
Other than that, I imagine that (on some browsers) there may be some performance gain too, since with user.name
browser has to lookup in context outside your function while using this.name
sticks to current context.
Sample Code
var user = {
name : 'John Doe',
show_name : function(){
alert(user.name);
}
};
var something = user; // Passing the object to another variable
user = null; // Make the user variable point to something else
something.show_name();
If you run the code above, you'll hit an error at the following line:
alert(user.name); // <-- user is null, so this will produce the following Error:
Uncaught TypeError: Cannot read property 'name' of null
Now, if we switch to this
instead of user
, our code will work:
var user = {
name : 'John Doe',
show_name : function(){
alert(this.name);
}
};
var something = user; // Passing the object to another variable
user = null; // Make the user variable point to something else
something.show_name();
The above code will work since this
will point to the object that executed the show_name
function (this can be found in the left side of the . dot method notation and it's the object pointed to by something
).
REFERENCES:
To learn more about how the execution context (in our example the object) affects the this binding, read this excerpt from the wonderful book You Don't Know JS by Kyle Simpson.
The following chapters thoroughly explain how this
behaves in various contexts (but the whole series is worth reading!):
Chapter 1: this Or That?
Chapter 2: this All Makes Sense Now!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With