-
Notifications
You must be signed in to change notification settings - Fork 8
Define
James edited this page May 30, 2017
·
4 revisions
Define
🎼
effortlessly create a completely transparent & customizable api
.extendGetSet(Array<string>): Chainable
- ☮️ extends
.extend
- adds
set
&get
+ name in camelCase - then uses Object.defineProprety: getter & setter, with some added helpers
const Chain = require('chain-able')
const chain = new Chain().extendGetSet(['ehOh'])
// can be used as normal object with getter/setters
chain.ehOh = false
const ehOh = chain.ehOh
ehOh == chain.ehOh == false
// when a value is passed in, it's a setter, same as using merge
chain.ehOh(true) === chain.setEhOh(true) === chain.merge({ehOh: true})
// when no value is passed in & it has no default,
// the result is the same as the getter
chain.ehOh() === chain.getEhOh() === true
.extendGetSet(Array<string>): Chainable
🍭 similar to extendGetSet
, but instead of creating methods, it decorates existing methods & scopes a reference to them
class Coolio extends Chain {
constructor(parent) {
super(parent)
// decorates `ref` method with getter/setter,
// with scoped reference to the method
this.defineGetSet(['ref'])
}
ref(arg) {
console.log(arg)
return this.set('ref', ref)
}
}
const coolio = new Coolio()
coolio.ref = 'eh' // logs eh
coolio.ref() // === 'eh'
coolio.ref // === 'eh'
coolio.ref('eh') // logs eh