0.0.2 • Published 7 years ago

extend-decorator v0.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

Extend Decorator npm version Build Status

Extend using a decorator!

Example

Given two classes Hi and Boo, extend Hello.

// part 1a
class Hi {
  sayHi () {
    return 'hi'
  }
}
export default Hi
// part 1b
class Boo {
  sayBoo () {
    return 'boo'
  }
}
export default Boo
// part 2
import extend from 'extend-decorator'
import Hi from './Hi'
import Boo from './Boo'

@extend(Hi)
class Hello {
  constructor ($hello) {
    $hello.innerHTML = this.sayHi()
  }
  sayHi () {
    return 'hello'
  }
}

@extend(Hi, Boo, true)
class HelloOverwrite {
  constructor ($helloOverwrite) {
    $helloOverwrite.innerHTML = `${this.sayHi()} and ${this.sayBoo()}`
  }
  sayHi () {
    return 'hello'
  }
}

If the last argument for @extend() is a boolean true, overwrite matching methods. To extend with more objects, just pass them as additional arguments i.e. @extend(A, B, C).