Last active
December 17, 2015 08:59
-
-
Save flexelektro/5583730 to your computer and use it in GitHub Desktop.
This is an Class Constructor without inheritance, but with modular private class and instance properties.
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 ClassMaker = function(conf,parent){ | |
/* conf = { | |
* privprops : {}, | |
* classprops : {}, | |
* iprops : {} | |
* } | |
*/ | |
var klass = function(){ | |
// Wird nur durchlaufen wenn Klasse erstellt wird | |
if(typeof klass.self !== "function"){ | |
var self = klass.self = klass; | |
var classinit = function(){ | |
self.addPrivateProps(conf.privprops); | |
self.addClassProps(conf.classprops); | |
self.addInstanceProps(conf.iprops); | |
} | |
// TodO private Methoden mit get set ? | |
// oder mit ner art proxy ? bind ?? apply ? | |
var _ = {} | |
self.addPrivateProps = function(o){ | |
for(var i in o){ | |
_[i] = o[i]; | |
} | |
} | |
self.addClassProps = function(o){ | |
for(var i in o){ | |
self[i] = o[i]; | |
} | |
} | |
self.addInstanceProps = function(o){ | |
for(var i in o){ | |
self.prototype[i] = o[i]; | |
} | |
} | |
self.prototype.init = function(){ console.log("Instance init Method")}; | |
this.klass = self; | |
classinit(); | |
} | |
this.init.apply(this,arguments); | |
} | |
return klass; | |
} | |
var FahrzeugClass = new ClassMaker({ | |
classprops : { | |
allitems : [], | |
showAllItems : function(){ | |
console.log("All:"); | |
console.log(this.allitems); | |
} | |
}, | |
iprops : { | |
data : {name : "",age : null}, | |
init : function(){ | |
this.data.name = arguments[0]; | |
this.klass.allitems.push(this); | |
}, | |
hupen : function(){ | |
console.log("tuttuut! Hier kommt "+ this.data.name); | |
}, | |
description : function(){ | |
console.log(this.__proto__); | |
} | |
} | |
},null); | |
var auto = new FahrzeugClass("Ford"); | |
auto.hupen(); | |
auto.description(); | |
FahrzeugClass.showAllItems(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment