Last active
May 26, 2019 08:19
-
-
Save lcherone/0b55b6947f0ee1eec69733ece848e4bb to your computer and use it in GitHub Desktop.
handy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A handy module pattern | |
*/ | |
const Module = (function () { | |
/* | |
** Private variables | |
*/ | |
let private = { | |
i: 0 | |
} | |
/** | |
* An instance based method which will set an | |
* immutable key and/or define data on itself | |
* | |
* - item = Module.item('key', {foo:'bar'}) // undefined | |
* - item = new Module.item('key', {foo:'bar'}) // {foo: 'bar'} | |
* | |
* Handy for things like, | |
* let row = new database.row('table', { | |
* foo: 'bar' | |
* }) | |
*/ | |
this.item = function (key, data) { | |
let item = this | |
if (data !== undefined) item = Object.assign(item, data) | |
// define an immutable key on the instance | |
Object.defineProperty(item, 'key', { | |
value: key, | |
writable: false, | |
enumerable: false | |
}) | |
} | |
/** | |
* A prototype method for the item instance | |
* | |
* let row = new database.row('table', { | |
* foo: 'bar' | |
* }) | |
* row.store() // noice | |
*/ | |
this.item.prototype.store = function () { | |
let row = this //? | |
this.key //? | |
return row | |
} | |
/** | |
* Something to increase which is from a private var | |
*/ | |
this.i = private.i | |
/** | |
* Increment the private var | |
*/ | |
this.ip1 = function () { | |
++private.i | |
return private.i | |
} | |
return this | |
})() //?. | |
// | |
let item = new Module.item('someItemKey', { | |
someProp: 'A value' | |
}) //?. | |
item.key //? | |
item.store() //? | |
/* | |
** var scopes | |
*/ | |
// local scoped | |
++Module.i //? | |
++Module.i //? | |
++Module.i //? | |
++Module.i //? | |
Module.i = 10 //? | |
// instance scope | |
Module.ip1() //? | |
Module.ip1() //? | |
Module.ip1() //? | |
Module.ip1() //? | |
// is 10 | |
Module.i //? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use Quokka.js..