Skip to content

Instantly share code, notes, and snippets.

@lcherone lcherone/index.js
Last active May 26, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.