Skip to content

Instantly share code, notes, and snippets.

@k33g
Created November 22, 2010 13:41
Show Gist options
  • Save k33g/709987 to your computer and use it in GitHub Desktop.
Save k33g/709987 to your computer and use it in GitHub Desktop.
js for oo javascript again
(function (){
/*mummy*/
var mum = {
types:new Array(),
interface:function(interface_def){
var interface_name = (new interface_def).constructor.name;
mum.types[interface_name]= new interface_def;
},
/*class def*/
class:function(p_class){
var constructor_name = (new p_class.$def).constructor.name;
//static members
if(p_class.$sta!=undefined){
for(var member in p_class.$sta){p_class.$def[member]=p_class.$sta[member];}
}
//inheritance
if(p_class.$ext!=undefined){
p_class.$def.prototype = new p_class.$ext;
p_class.$def.prototype.parent = new p_class.$ext;
p_class.$def.parent = new p_class.$ext;
}
//save constructor name for get instance of
p_class.$def.prototype.getTypeName = function(){return constructor_name};
p_class.$def.getTypeName = function(){return constructor_name};
//interface
if(p_class.$imp!=undefined){
var inst = new p_class.$def;
p_class.$def.prototype.interfaces = p_class.$imp;
p_class.$def.interfaces = p_class.$imp;
for(var i=0; i < p_class.$imp.length; i++){
for(var member in p_class.$imp[i]){
if(inst[member]!=undefined){
if(typeof(inst[member]) != typeof(p_class.$imp[i][member]) ){
/*implemented but bad type*/
throw 'Class '+constructor_name+', Interface '+p_class.$imp[i].constructor.name+
' : bad type : '+member+
' implemented, but '+
typeof(inst[member])+' instead of '+
typeof(p_class.$imp[i][member]);
}
}else{
/*not implemented*/
throw member+' not implemented in Class '+constructor_name+
', Interface '+p_class.$imp[i].constructor.name;
}
}
}
}//end interface
//record new type in types list
mum.types[constructor_name]= p_class.$def;
},
/*get instance of class with params*/
select:function(class_def){
var instance = new class_def;
return new function(){
this.new=function(){
instance[instance.getTypeName()].apply(instance,arguments);/*mettre un trycatch ou faire un test*/
//instance['getTypeName'] = function(){return this.className;}
return instance;
}
}
}
}/*END var mum*/
if(!window.$class){window.$class=mum.class;}
if(!window.$interface){window.$interface=mum.interface;}
if(!window.$get){window.$get=mum.select;}
if(!window.$){window.$=mum.types;}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment