user3293653
user3293653

Reputation:

Should I use the optional context parameter if I don't use `this` keyword in the callback?

when using the multitude of utility functions that can accept it. For example:

function foo () {
    _.each ([0,1,2,3], function(val) {
        // I don't use this in the body
    }, this);
}

It seems like one way is more concise and the other is there in case you add code later.

Upvotes: 0

Views: 48

Answers (2)

Bergi
Bergi

Reputation: 664599

It seems like one way is more concise

Then use that.

in case you add code later.

You ain't gonna need it. Not until later, at least.

Upvotes: 1

Trace
Trace

Reputation: 18869

The optional parameter adds the context, in other words what 'this' refers to within the callback.
You only need to add it if you want it that way. If you're using a library like backbone, it can come in very handy (eg. when the function is called from within a view object, and you want 'this' to refer to the view).

Upvotes: 1

Related Questions