Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript 'this' versus object name

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);
    }
};
like image 359
Fluidbyte Avatar asked Oct 09 '12 12:10

Fluidbyte


People also ask

What is the this keyword in JavaScript?

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.

Why do we use this in JavaScript?

“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.

What does $() mean in JavaScript?

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").


2 Answers

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.

like image 80
Sirko Avatar answered Oct 20 '22 01:10

Sirko


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!

like image 40
WTK Avatar answered Oct 19 '22 23:10

WTK