1.0.0 • Published 6 years ago

es6-class-bind-all v1.0.0

Weekly downloads
52
License
MIT
Repository
github
Last release
6 years ago

Problem

As you see, too many .bind() operations in constructor. Once you have tons of classes are called in callback or whatever something change it's scope, it will be horribly troublesome.

class Person {
  constructor() {
    this.name = 'Steve'
    this.age = 20
    this.gender = 'male'

    this.setName = this.setName.bind(this)
    this.setAge = this.setAge.bind(this)
    this.setGender = this.setGender.bind(this)
  }
  setName(name) {
    this.name = name
  }
  setAge(age) {
    this.age = age
  },
  setGender(gender) {
    this.gender = gender
  }
}

Easy Way

Bind all methods in constructor.

import es6ClassBindAll from 'es6-class-bind-all'

class Person {
  constructor() {
    this.name = 'Jack'
    this.age = 20
    this.gender = 'male'

    es6ClassBindAll(this)
  }
  setName(name) {
    this.name = name
  }
  setAge(age) {
    this.age = age
  },
  setGender(gender) {
    this.gender = gender
  }
}

Or you can bind specified methods in constructor depend on your situation.

es6ClassBindAll(this, ['setGender'])

Install

yarn add es6-class-bind-all --dev

or npm

npm install es6-class-bind-all --save-dev

License

The MIT License (MIT). Please see License File for more information.

1.0.0

6 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago