Skip to content

Instantly share code, notes, and snippets.

@bpocallaghan
Created March 20, 2018 08:05
Show Gist options
  • Save bpocallaghan/a4628e0f6cfdb8d19469fc8187122512 to your computer and use it in GitHub Desktop.
Save bpocallaghan/a4628e0f6cfdb8d19469fc8187122512 to your computer and use it in GitHub Desktop.
Javascript Class.js snippet
/*
* 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