Skip to content

Instantly share code, notes, and snippets.

@rednebmas
Created February 4, 2017 23:19
Show Gist options
  • Select an option

  • Save rednebmas/9d218a1c6ea06a64f1bab6d34fee86f6 to your computer and use it in GitHub Desktop.

Select an option

Save rednebmas/9d218a1c6ea06a64f1bab6d34fee86f6 to your computer and use it in GitHub Desktop.
Neat javascript object/class pattern
var myClass = function(constructorParam) { return {
/** object notation properties **/
x: "it ",
_state: 'awesome',
/** Getters and setters! **/
get state() {
return this._state;
},
set state(value) {
this._state = value;
},
/** init method **/
init: function() {
this.y = this.x + constructorParam;
return this;
}
}.init(); }
var objInstance = new myClass("works!");
console.log(objInstance.y); // it works!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment