Created
October 25, 2012 18:49
-
-
Save bmatusiak/3954638 to your computer and use it in GitHub Desktop.
My Class Concept
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
var classify = { | |
name : "Classify", | |
version : "1.0", | |
_defined:{}, | |
define:function(objectName,object){ | |
this._defined[objectName] = object; | |
}, | |
ify:function(Class){ | |
function F() { | |
if(!this._parents)this._parents=[]; | |
for(var i = 0; i < this._parents.length; i++) | |
this._parents[i].apply(this, arguments); | |
this.constructor.apply(this, arguments); | |
} | |
F.prototype = this.Extend(Class,this.combine(Class.extend,Class.extendOverLoad)); | |
return F; | |
}, | |
combine:function(objArray,overload,defs) { | |
if(!defs) defs = {}; | |
defs._parents = []; | |
for(var j in objArray){ | |
var props = objArray[j]; | |
if(typeof(objArray[j]) == 'string'){ | |
props = this._defined[objArray[j]]; | |
}else{ | |
props = objArray[j]; | |
} | |
for (var key in props) { | |
if(key == "extend") | |
this.combine(props[key],props.overload,defs); | |
else | |
if(key == "constructor")defs._parents.push(props[key]); | |
else | |
if (props.hasOwnProperty(key) && !defs.hasOwnProperty(key) || overload && props.hasOwnProperty(key)){ | |
defs[key] = props[key]; | |
} | |
} | |
} | |
return defs; | |
}, | |
Extend:function(props, parent) { | |
var defs = {}; | |
for (var key in props) { | |
if (props.hasOwnProperty(key)) { | |
defs[key] = { | |
value: props[key], | |
enumerable: true | |
}; | |
} | |
} | |
var newObject = Object.create(parent, defs); | |
return newObject; | |
} | |
}; | |
var moduleClass = classify.ify(classify); | |
moduleClass.prototype.Classify = classify; | |
module.exports = new moduleClass(); |
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
SlaveClass 1constructor lol | |
SlaveClass 2constructor lol | |
SlaveClass 3constructor lol | |
SlaveClass v1.3 | |
SlaveClass a | |
SlaveClass b | |
SlaveClass c | |
{} |
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
var classify = require("./classify.js"); | |
classify.define("MasterClass",{ | |
constructor:function(arg){ | |
console.log("1constructor",arg); | |
}, | |
a:function(){ | |
console.log("a"); | |
} | |
}); | |
var SweetClass = { | |
constructor:function(arg){ | |
console.log("2constructor",arg); | |
}, | |
b:function(){ | |
console.log("b"); | |
} | |
}; | |
var SlaveClass = { | |
extend: ["MasterClass",SweetClass], | |
constructor:function(arg){ | |
console.log("3constructor",arg); | |
}, | |
c:function(){ | |
console.log("c"); | |
} | |
}; | |
var classify = require("./classify.js"); | |
var testClass = classify.ify(SlaveClass); | |
//class is built | |
//init class | |
var clas = new testClass("lol"); | |
clas.a(); | |
clas.b(); | |
clas.c(); | |
console.log(clas); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs
childClass loaded
childClass slaveClass