Skip to content
James edited this page May 30, 2017 · 4 revisions

Define 🎼

effortlessly create a completely transparent & customizable api

🌐 api

extendGetSet

syntax

.extendGetSet(Array<string>): Chainable

overview
example
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

defineGetSet

syntax

.extendGetSet(Array<string>): Chainable

overview

🍭 similar to extendGetSet, but instead of creating methods, it decorates existing methods & scopes a reference to them

example
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

🔗 related

Clone this wiki locally