Created
March 20, 2018 08:05
-
-
Save bpocallaghan/a4628e0f6cfdb8d19469fc8187122512 to your computer and use it in GitHub Desktop.
Javascript Class.js snippet
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
/* | |
* MyClass | |
*/ | |
var MyClass = function (options) | |
{ | |
/* | |
* Variables accessible | |
* in the class | |
*/ | |
var vars = { | |
myVar: 'original Value' | |
}; | |
/* | |
* Can access this.method | |
* inside other methods using | |
* root.method() | |
*/ | |
var root = this; | |
/* | |
* Constructor | |
*/ | |
this.construct = function (options) | |
{ | |
$.extend(vars, options); | |
}; | |
/* | |
* Public method | |
* Can be called outside class | |
*/ | |
this.myPublicMethod = function () | |
{ | |
console.log(vars.myVar); | |
myPrivateMethod(); | |
}; | |
/* | |
* Private method | |
* Can only be called inside class | |
*/ | |
var myPrivateMethod = function () | |
{ | |
console.log('accessed private method'); | |
}; | |
/* | |
* Pass options when class instantiated | |
*/ | |
this.construct(options); | |
}; | |
/* | |
* USAGE | |
*/ | |
/* | |
* Set variable myVar to new value | |
*/ | |
var newMyClass = new myClass({myVar: 'new Value'}); | |
/* | |
* Call myMethod inside myClass | |
*/ | |
newMyClass.myPublicMethod(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment