The bindAll
function from the Underscore.js library allows you to bind the this
context of multiple methods in an object to that object. This can be useful, for example, when working with event handlers that need to reference the associated object instance.
Here is an example of how to use bindAll
:
index.tsx407 chars21 lines
In this example, we create an object with two methods, logName
and logFoo
. We then bind these methods using bindAll
. This allows us to use the methods without worrying about the this
context.
Note that we pass the method names as additional arguments to bindAll
. If we had many methods to bind, we could pass them as an array instead.
index.tsx39 chars2 lines
Also note that bindAll
modifies the original object, so use it carefully.
gistlibby LogSnag