Created
October 28, 2012 05:18
-
-
Save bmatusiak/3967704 to your computer and use it in GitHub Desktop.
My Class Concept2
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
Function.prototype.extend = function(_class, subClass) { | |
if (!subClass) { | |
return _Class(Object, _class); | |
} | |
else { | |
return _Class(_class, subClass); | |
} | |
}; | |
function _Class(parent, child) { | |
if(!parent._this)parent._this = parent; | |
var parentConstro = parent.prototype.constructor; | |
function Empty() {} | |
Empty.prototype = parent.prototype; | |
parent.prototype.constructor = Empty; | |
var proto = new Empty(); | |
for (var namei in parent.prototype) { | |
proto[namei] = parent.prototype[namei]; | |
} | |
var baseFN = function(p, c) { | |
if(!c){ | |
return function() { | |
if (this.base) delete this.base; | |
return p.apply(this, arguments); | |
}; | |
}else{ | |
return function() { | |
if (p) this.base = p; | |
var ret = c.apply(this, arguments); | |
delete this.base; | |
return ret; | |
}; | |
} | |
}; | |
for (var name in child.prototype) { | |
if (typeof child.prototype[name] == "function" && typeof parent.prototype[name] == "function") { | |
proto[name] = baseFN(parent.prototype[name], child.prototype[name]); | |
} | |
else if (typeof child.prototype[name] == "function" && !parent.prototype[name]) { | |
proto[name] = baseFN(child.prototype[name]); | |
} | |
else { | |
proto[name] = child.prototype[name]; | |
} | |
} | |
var constro = baseFN(parentConstro, child.prototype.constructor); | |
function Class() { | |
if (constro) constro.apply(this, arguments); | |
} | |
Class.prototype = proto; | |
Class.prototype.constructor = Class; | |
child._parent = parent._this; | |
Class._this = child; | |
Class.prototype._this = child; | |
Class.extend = function(subClass) { | |
return Function.prototype.extend(Class, subClass); | |
}; | |
return Class; | |
} |
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
require("./extend.js"); | |
function _SuperClass() { | |
if (this.base) this.base(); | |
this.testA = "test1"; | |
console.log("SuperClass constructor"); | |
} | |
_SuperClass.prototype.t1 = function() { | |
if (this.base) this.base(); | |
console.log("t1"); | |
}; | |
var SuperClass = _SuperClass.extend(Array,_SuperClass); | |
function _ClassT() { | |
if (this.base) this.base(); | |
this.testB = "test2"; | |
console.log("Class constructor"); | |
} | |
_ClassT.prototype.t1 = function() { | |
console.log("t2"); | |
if (this.base) this.base(); | |
}; | |
var ClassT = SuperClass.extend(_ClassT); | |
function _SubClass() { | |
if (this.base) this.base(); | |
this.testB = "test3"; | |
console.log("SubClass constructor"); | |
} | |
_SubClass.prototype.t1 = function() { | |
if (this.base) this.base(); | |
console.log("t3"); | |
}; | |
var SubClass = ClassT.extend(_SubClass); | |
//Following produces | |
/* | |
SuperClass constructor | |
Class constructor | |
SubClass constructor | |
class init time took 11 milisec | |
t2 | |
t1 | |
t3 | |
*/ | |
var doneTime,startTime; | |
startTime= new Date(); | |
var testit = new SubClass(); | |
doneTime = new Date(); | |
console.log("class init time took ",doneTime - startTime, "milisec"); | |
testit.t1(); | |
console.log(testit); //{ testA: 'test1', testB: 'test3' } | |
console.log(testit._this); //{ [Function: _SubClass] _parent: { [Function: _ClassT] _parent: { [Function: _SuperClass] _parent: [Object] } } } | |
console.log(testit._this._parent); //{ [Function: _ClassT] _parent: { [Function: _SuperClass] _parent: { [Function: Function] _this: [Circular] } } } | |
//Following produces | |
/* | |
_SubClass is not Function | |
_SubClass is a Object | |
_SubClass is not Date | |
_SubClass is a Array | |
_SubClass is not Boolean | |
_SubClass is not String | |
_SubClass is not RegExp | |
_SubClass is not Number | |
*/ | |
var Types = {Function:Function,Object:Object,Date:Date,Array:Array,Boolean:Boolean,String:String,RegExp:RegExp,Number:Number}; | |
for(var i in Types){ | |
console.log(testit._this.name+" is " +(testit instanceof Types[i] ? "a" :"not" )+" "+i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment