Code Monkey home page Code Monkey logo

bindall-standalone's Introduction

bindall-standalone

Allow to permanently mutate an object's method so it context this will always be bound to this object.

Allow to avoid the non-unbindable listeners registered via emitter.on(foo, this.bar.bind(this));.

Installation

npm install bindall-standalone --save

Then just var bindAll = require('bindall-standalone'). Works with require() e.g. node.js, browserify or component(1).

API

bindAll(object, *methods);

Mutates all methods from object, passed as a list of strings (such as 'foo', 'bar') so they always will be called with the context bound to the object.

bindAll(object);

Bind ALL methods available on the object.

Usage

Basic example

var bindAll = require('bindall-standalone');

var object = {
    foo: 10,
    bar: function() {
        return this.foo;
    }
};

object.bar(); // 10

var func = object.bar;
func(); // undefined

bindAll(object, 'bar');
var func = object.bar;
func(); // 10

Real-world example

var bindAll = require('bindall-standalone');

var Foo = function() {
    bindAll(this, 'onReady');

    // mediator.on('ready', this.onReady.bind(this)); // Never going to be unbinded !
    mediator.on('ready', this.onReady); // No need for explicit 'bind' now !
};

Foo.prototype.onReady = function() {
    // mediator.off('ready', this.onReady.bind(this)); // That is sad, bro
    mediator.off('ready', this.onReady); // Properly unbinded !
};

What is this

It used to be a standalone version of underscore's _.bindAll() function for IE9+ browsers. But since bindAll goal is to provide a quick way to bind/unbind methods, and only that, I updated it to use a quicker, more compatible bind function.

See the underscore source for reference.

Basically, it avoids this use case:

mediator.on('foo', this.bar.bind(this));
mediator.off('foo', this.bar.bind(this));
// will never be unbinded because this.bar.bind(this) != this.bar.bind(this)

Under the hood

Since bindAll's only goal is to bind a method to its object context, the bind function can be written as:

function bind(func, context) {
  return function() {
    return func.apply(context, arguments);
  };
}

No need for the modern bind function, no need for a polyfill. And it's a lot faster ! Check this jsPerf case.

Caveats

There is one significant thing to know: by binding a method on it's object context, it creates an instance-level method. Check this article and this fiddle for more info.

bindall-standalone's People

Contributors

ayamflow avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.