Giter VIP home page Giter VIP logo

auto-bind-inheritance's Introduction

auto-bind-inheritance

Automatically bind methods to their class instance and works on parent methods.

This package was created directly as a result of failing on extended classes in the auto-bind package.

There are issues with getters which are initialized in the constructor:

  1. initialize the getter/setter inside the getter/setter (not ideal)
  2. call autoBindI at end of final child (not ideal)
  3. manually call autoBindI
  4. avoid getter/setters

Install

$ npm install --save auto-bind-inheritance

Usage

// const autoBind = require('auto-bind'); // drop-in replacement
const autoBindI = require('auto-bind-inheritance');

class A {
	constructor() {
		this.val = 'A';
		autoBindI(this);
	}
	method2() { console.log('A.method2 ', this.val); }
}
  
class B extends A {
	constructor() {
		super();
		this.val = 'B';
	}
	method1() { console.log('B.method1 ', this.val); }
}


let a = new A();
let b = new B();


b.method1();
b.method2();
let mb1 = b.method1;
let mb2 = b.method2;
console.log(mb1.name);
console.log(mb2.name);
mb1();
mb2();


// With `autoBind(this)`, the above could have resulted in:
//=> 'B.method1  B'
//=> 'A.method2  B'
//=> 'bound bound method1'
//=> 'method2'
//=> 'B.method1  B'
//=> 'exception "Cannot read property 'val' of undefined"'

// With `autoBindI(this)`, the above now correctly results in bound and callable methods:
//=> 'B.method1  B'
//=> 'A.method2  B'
//=> 'bound method1'
//=> 'bound method2'
//=> 'B.method1  B'
//=> 'A.method2  B'

API

autoBind(self)

Bind methods in self to their class instance. Returns the self object. Can be placed in the parent and will be call on all child classes because the prototype chain has already been created.

self

Type: Object

Object with methods to bind.

Related

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

License

MIT © Chad Wingrave

auto-bind-inheritance's People

Contributors

cwingrav avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

xemasiv

auto-bind-inheritance's Issues

Bug with getter and setter methods

The code is executing the getter method but the class is not yet initialized.

const autoBindI = require('auto-bind-inheritance');

class A {
   constructor() {
       autoBindI(this);
   }
}

class B extends A {
    constructor() {
         super();
         this.foo = [];
    }

    // when `autoBindI` detects this method, this.foo is still undefined
    // `self[key] = val.bind(self);` will now result in
    // "Cannot read property '0' of undefined"
    get bar() {
        return this.foo[0];
    }
}

new B();

I think it should skip getter and setter methods.

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.