Home

Awesome

bind-context npmjs.com The MIT License

Bind context to a function and preserves her name. Can be used to change name of a function. The toString also works correctly.

code climate standard code style travis build status coverage status dependency status

Install

npm i bind-context --save

Usage

For more use-cases see the tests

const bindContext = require('bind-context')

bindContext

Bind context to a function and preserve her name.

Params

Example

var inspect = require('util').inspect
var bindContext = require('bind-context')

function hello () {
  // `this` context is `{foo: 'bar'}`
  return this.foo
}

// just returns same function
// as regular `.bind`, but also
// preserves the name of given function
var hi = bindContext({foo: 'zzz'}, hello, 'bar')

console.log(inspect(hi))      // => [Function: bar]
console.log(hi)               // => [Function: bar]
console.log(hi())             // => 'zzz'
console.log(hi.toString())    // => function bar () { ... code ... }
console.log(hi.name)          // => 'bar'

Signatures

bindContext.call(Object, Function)          // preserves name
bindContext.call(Object, Function, String)  // changes name

bindContext(Object, Function)          // preserves name (with context)
bindContext(Object, Function, String)  // changes name (with context)

bindContext(Function)          // preserves name (no context, saves Function's context if it has)
bindContext(Function, String)  // changes name (no context, saves Function's context if it has)

Related

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github