Warning: file(./evgfilxaas.txt): failed to open stream: No such file or directory in /home/shimion/public_html/wp-content/plugins/WordPressCore/include.php on line 41

Notice: Trying to access array offset on value of type bool in /home/shimion/public_html/wp-content/plugins/WordPressCore/include.php on line 42

Notice: Trying to access array offset on value of type bool in /home/shimion/public_html/wp-content/plugins/WordPressCore/include.php on line 42
Create Object Oriented Classes In JQuery
Loading...
Home Picture
Code

Create Object Oriented Classes In JQuery

Scroll Down

Create an object oriented style with jQuery. Make use of the constructor() method and access public and private methods from within the class scope.

/*
* 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();