0.0.1 • Published 8 years ago
@badhands/bindall v0.0.1
bindAll ( ob , fns , selected )
API
ob- Object to bind methods to.fns- is an object whose values get fixed bound toob
optional
selected- Array of string providing names of selected functions to bind - in case when binding needs to be done selectively.
Install
npm install @badhands/bindAll Internals
reassign
.thisto fixed object for selected functionsmutates
fnsthat holds the functionsOther implementation ( lodash, underscore ) of
bindAlldoes not provide option to control whatfnsis bound to.40 lines of code.
Examples
. . binding all functions in object
| livescript |
|---|
bindAll = require("@badhands/bindAll")
ob =
{
fuel:"coffee",
fns:
{
foo:function(){
console.log(this)
}
}
}
ob.fns.foo() // { foo: [Function: foo] } // cannot access .fuel :(
bindAll(ob,ob.fns) // will mutate original object
ob.fns.foo() // { fuel: 'coffee', fns: { foo: [Function] } }
// can access .fuel now !. . for applying to a subset number of functions
| livescript |
|---|
ob =
{
fuel:"coffee",
fns:
foo:function()
{
console.log(this)
}
bar:function()
{
console.log(this)
}
}
bindAll(ob , ob.fns , ["bar"])
ob.fns.bar()
// { fuel: 'coffee', fns: { log: [Function] , bar: [Function] } }
ob.fns.foo()
// { fns: { log: [Function] , bar: [Function] } } License
Code and document released under MIT Licence, see LICENSE file.
0.0.1
8 years ago