Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active May 26, 2019 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/0b55b6947f0ee1eec69733ece848e4bb to your computer and use it in GitHub Desktop.
Save lcherone/0b55b6947f0ee1eec69733ece848e4bb to your computer and use it in GitHub Desktop.
handy
/**
* 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 //?
@lcherone
Copy link
Author

lcherone commented May 26, 2019

Use ​​​​Quokka.js..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment