On Github shrunyan / sdjs-delegate-prototypes
Created by @stuartrunyan
* p.s. I'll be using ES6 syntax
// exports ES6 module export default { get(key) { return this[key] }, set(key, val) { this[key] = val return this[key] } }
* Bonus points: Wrap your store in an observable
import Store from './Store' function TimeStore () { // Object.assign concats our objects together // Object.create sets Store as prototype of new object return Object.assign(Object.create(Store), { isSet(key) { return this[key] ? true : false }, getEpoch(key) { return new Date(this.get(key)).getTime() } }) } export default TimeStore
import TimeStore from './TimeStore' const store = TimeStore() store.isSet('currentTime') // false store.set('currentTime', Date()) // timestamp store.isSet('currentTime') // true store.get('currentTime') // timestamp store.getEpoch('currentTime') // timestamp
Code syntax highlighting courtesy of highlight.js.