Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bbmoz/extend-decorator

Extend your objects using a decorator!
https://github.com/bbmoz/extend-decorator

annotation babel decorator dom es2016 es7 extend inject

Last synced: 5 days ago
JSON representation

Extend your objects using a decorator!

Awesome Lists containing this project

README

        

# Extend Decorator
[![npm version](https://badge.fury.io/js/extend-decorator.svg)](https://badge.fury.io/js/extend-decorator)
[![Build Status](https://travis-ci.org/bbmoz/extend-decorator.svg)](https://travis-ci.org/bbmoz/extend-decorator)

> Extend using a decorator!

## Example

Given two classes *Hi* and *Boo*, extend *Hello*.

```javascript
// part 1a
class Hi {
sayHi () {
return 'hi'
}
}
export default Hi
```

```javascript
// part 1b
class Boo {
sayBoo () {
return 'boo'
}
}
export default Boo
```

```javascript
// 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() // hello
}
sayHi () {
return 'hello'
}
}

@extend(Hi, Boo, true)
class HelloOverwrite {
constructor ($helloOverwrite) {
$helloOverwrite.innerHTML = `${this.sayHi()} and ${this.sayBoo()}` // hi and boo
}
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)`.