Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding "this" keyword

In this commit there is a change I cannot explain

deferred.done.apply( deferred, arguments ).fail.apply( deferred, arguments );

becomes

deferred.done( arguments ).fail( arguments );

AFAIK, when you invoke a function as a member of some object like obj.func(), inside the function this is bound to obj, so there would be no use invoking a function through apply() just to bound this to obj. Instead, according to the comments, this was required because of some preceding $.Callbacks.add implementation.

My doubt is not about jQuery, but about the Javascript language itself: when you invoke a function like obj.func(), how can it be that inside func() the this keyword is not bound to obj?

like image 992
Raffaele Avatar asked Jun 25 '12 08:06

Raffaele


People also ask

What do you understand by this keyword 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 is this keyword in node JS?

“this” Refers to a Global Object The window object is the global object in the case of the browser. And in a NodeJS environment, a special object called global will be the value of this .

Is this keyword important in JavaScript?

But it is critically important for writing more advanced JavaScript code. In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword.

What is this in a function?

“this” inside FunctionsThe value of this inside a function is usually defined by the function's call. So, this can have different values inside it for each execution of the function. In your index. js file, write a very simple function that simply checks if this is equal to the global object.


2 Answers

My doubt is not about jQuery, but about the Javascript language itself: when you invoke a function like obj.func(), how can it be that inside func() the this keyword is not bound to obj?

Well, the only way this is possible is if obj.func references a bound function, then it doesn't matter how you call it. In that case it doesn't matter how you call the function, whether you do obj.func(), func(), func.call({}), func.apply({}) doesn't matter at all. I'm not sure how the commit is related to this, however.

To clarify, I am answering the quoted question interpreted as:

Given a call signature like: obj.func(), how is it possible that this is not obj inside the called function for the call?

like image 81
Esailija Avatar answered Sep 20 '22 13:09

Esailija


While the other answers deal with your question about the this keyword, they don't answer your question of why apply is being used. Here's the kicker: apply is not being used to set the this keyword in that example. Instead, it's being used to pass arguments as the arguments of the done and fail functions.

Take this example:

var x = {
    a: function(a1, a2, a3) {
        console.log(a1, a2, a3);
    }
}

function callA() {
    x.a.apply(x, arguments);
    x.a(arguments);
}

callA('red', 'blue', 'green');

If you execute this code you should see the difference between x.a.apply(x, arguments); and x.a(arguments);. In the first one, a1, a2, and a3 are the respective colors "red", "blue", and "green". In the second one, a1 is an array-like object [ "red", "blue", "green" ] and a2 and a3 are undefined.

Likewise, in your posted example, apply is being used to pass the arguments object correctly, not to set the this keyword.

like image 26
Nathan Wall Avatar answered Sep 21 '22 13:09

Nathan Wall