Allain Lalonde
Allain Lalonde

Reputation: 93408

What is the purpose setting a bound function with the same name?

Can anyone explain to me the purpose of JavaScript code like this:

this._handleBlah = this._handleBlah.bind(this);

It appears in a constructor and this pattern seems to be used across a code base I'm reading.

Upvotes: 0

Views: 26

Answers (1)

Pointy
Pointy

Reputation: 413826

It's useful when the "member" functions are going to be used as event handlers, or in other similar callback situations. The bound versions will have the correct this value.

Upvotes: 2

Related Questions