Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to bind this or use a variable? [closed]

Tags:

People also ask

When would you use the bind function?

JavaScript Function bind() With the bind() method, an object can borrow a method from another object. The example below creates 2 objects (person and member).

Should you use BIND in JavaScript?

. bind() is used when you need to pass a callback (e.g. some sort of function reference), but you want the caller to call your function with a specific this value.

What is the point of bind?

A bind point refers to a place in a video game where a character will be sent to after using a particular item. Depending on the video game, the bind point is a specific building, a boat, a particular landmark or a specific set of coordinates.

Does bind create a closure?

bind() creates a closure itself so don't be thinking that using . bind() is eliminating a closure. It is eliminating you having to hand-code a closure variable which is what makes it handy to use and makes your code look cleaner.


Is it better to do,

asset.addEventListener("load", function () {
  this.emit({
    type: "load",
    asset: asset
  });
}.bind(this), false);

Or

var scope = this;

asset.addEventListener("load", function () {
  scope.emit({
    type: "load",
    asset: asset
  });
}, false);

Is it better to bind the function, or just store the reference to this in a variable?