Created
February 12, 2011 10:44
-
-
Save k33g/823684 to your computer and use it in GitHub Desktop.
une autre version de classe en js avec héritage
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 mum = (function () { | |
var mummy = {}; | |
mummy.Class = function(definition){ | |
var that = function(){},m,mb,mbs,mbc,inst; | |
for(m in definition){ | |
switch(m){ | |
case 'Members':for(mb in definition.Members){that.prototype[mb] = definition.Members[mb];}break; | |
case 'Static':for(mbs in definition.Static){that[mbs] = definition.Static[mbs];}break; | |
case 'Extends':that.prototype = new definition.Extends;break; | |
case 'Init': | |
for(mbc in definition.Init){ | |
that.prototype.typeName = mbc; | |
if(definition.Init[mbc]!=null){ | |
that.prototype[mbc] = definition.Init[mbc]; | |
that.getInstance = function(){ | |
inst = new that; | |
inst[mbc].apply(inst,arguments); | |
return inst; | |
}; | |
}else{ | |
that.getInstance = function(){ | |
throw 'abstract class'; | |
}; | |
} | |
} | |
break; | |
default:/*nothing*/break; | |
} | |
} | |
return that; | |
} | |
return mummy; | |
}()); | |
And this is possible too :
Model.prototype.hello = function(){console.log("Hello World !!!");}
bob.hello(); /*bob says "Hello World !!!"*/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it like that :