Created
February 4, 2017 23:19
-
-
Save rednebmas/9d218a1c6ea06a64f1bab6d34fee86f6 to your computer and use it in GitHub Desktop.
Neat javascript object/class pattern
This file contains hidden or 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
| 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