Created
January 21, 2011 10:59
-
-
Save zohararad/789538 to your computer and use it in GitHub Desktop.
Simply OOP capabilities in vanilla Javascript
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
/** | |
* Classify.js | |
* A simple implementation of a Class native in Javascript with support for private and public methods and properties. | |
* Inspired by MooTools Class.js and by the Module Javascript design pattern (for private and public methods and properties) | |
* | |
* Zohar Arad. February 2011 | |
* | |
* Usage: | |
* | |
* var MyClass = new Class({ | |
* init:function(){ | |
* console.log('Initializing class instance'); | |
* }, | |
* _name:'Zohar Arad', | |
* speak:function(){ | |
* console.log(this._name); //outputs the value of private property this._name | |
* } | |
* } | |
* }); | |
* | |
* var c = new MyClass(); | |
* c.speak(); //will output 'Zohar Arad' | |
* console.log(c._name); //will outout nothing.... | |
*/ | |
(function(){ | |
var Function = this.Function; | |
if(typeof Function.bind !== 'function'){ | |
Function.prototype.bind = function(scope){ | |
var _self = this; | |
return function(){ | |
return _self.apply(scope,arguments) | |
}; | |
} | |
} | |
var Class = this.Class = function(defs){ | |
return function(){ | |
var _self = arguments.callee, self = this; | |
for(var def in defs){ | |
if(defs.hasOwnProperty(def)){ | |
var d = defs[def]; | |
if(def.indexOf('_') === 0){ | |
if(typeof(d) === 'function'){ | |
_self[def] = d.bind(defs); | |
} else { | |
_self[def] = d; | |
} | |
} else { | |
self[def] = d.bind(defs); | |
} | |
} | |
} | |
return this.init ? this.init.apply(self,arguments) : this; | |
} | |
} | |
Function.prototype.implement = function(defs){ | |
for(var def in defs){ | |
if(defs.hasOwnProperty(def)){ | |
this.prototype[def] = defs[def]; | |
} | |
} | |
return this; | |
} | |
Function.prototype.extend = function(defs){ | |
for(var def in defs){ | |
if(defs.hasOwnProperty(def)){ | |
this[def] = defs[def]; | |
} | |
} | |
return this; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment