Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    bind-methodspublic

    bind-methods Build Status

    Bind all methods in an object to itself or a specified context

    Install

    $ npm install --save bind-methods

    Usage

    const bindMethods = require('bind-methods');
     
    const unicorn = {
        name: 'Rainbow',
        message() {
            return `${this.name} is awesome!`;
        }
    };
     
    const message = unicorn.message;
    message();
    //=> Error: Cannot read property 'name' of undefined 
     
    bindMethods(unicorn);
     
    const message2 = unicorn.message;
    message2();
    //=> 'Rainbow is awesome!' 

    API

    bindMethods(input, [context])

    Bind methods in input to itself or context if specified. Returns the input object.

    input

    Type: Object

    Object with methods to bind.

    context

    Type: Object
    Default: The input object

    Object to bind the methods to.

    Related

    • auto-bind - Automatically bind methods to their class instance

    License

    MIT © Sindre Sorhus

    install

    npm i bind-methods

    Downloadslast 7 days

    2

    version

    1.1.0

    license

    MIT

    repository

    github.com

    last publish

    collaborators

    • avatar