Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of "this" as a function?

I inherited some code with a line that I don't understand.

function updateQty () {
        obj.find('.inputAmount').html(qty);
        input.val(qty);

        $.each(change_actions, function() {
            this(qty);
        });
    }

What exactly is happening inside the .each function? I've never seen this(var) used this way before.

like image 711
EmmyS Avatar asked Jul 24 '13 19:07

EmmyS


People also ask

What is this in function?

The this keyword refers to the object the function belongs to, or the window object if the function belongs to no object. It's used in OOP code, to refer to the class/object the function belongs to For example: function foo() { this.

Can we use this in arrow function?

In short, with arrow functions there are no binding of this . In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions the this keyword always represents the object that defined the arrow function.

When should we use this in JS?

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. Identifying the object in the current execution context when we invoke a method.


3 Answers

this inside of the $.each refers to the current objects that you are looping through.

The object must be a function in order to pass something to it.

like image 103
Naftali Avatar answered Oct 20 '22 19:10

Naftali


the author setup their own events with bindings, so chances are change_actions are functions that are subscribed to run when something happens to the quantity.

Try something like this:

// initialize with a value
var actions = [
    function(x){ console.log('I see a new x: ' + x); }
];

// add actions "later"
actions.push(function(x){ console.log('Yup, a new x: ' + x); });

// Then execute them:
$.each(actions, function(){
  this(4);
});

// add another one still
actions.push(function(x){ console.log(x + ' looks new'); });

// re-iterate over them
// Then execute them:
$.each(actions, function(){
  this(5);
});

and the result:

// first iteration (only 2 subscribed events)
[15:56:50.030] "I see a new x: 4"
[15:56:50.030] "Yup, a new x: 4"

// second iteration (now we have 3, one was added later)
[15:56:50.030] "I see a new x: 5"
[15:56:50.030] "Yup, a new x: 5"
[15:56:50.030] "5 looks new"  // <-- new subscription

think of it like the click event and how you can add subscriptions by binding to $('element').click(). every time a click happens, any subscribed events get triggered.

like image 32
Brad Christie Avatar answered Oct 20 '22 19:10

Brad Christie


You can relate to the following example:

var change_actions = [
    function(x) { alert(x + 1); },
    function(x) { alert(x + 2); }
];

var qty = 5;
$.each(change_actions, function() {
   this(qty); 
});

JSFiddle: http://jsfiddle.net/fuyz2/

like image 26
Kevin Le - Khnle Avatar answered Oct 20 '22 19:10

Kevin Le - Khnle