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).
. 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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With