Too much recursion 

Higher Order Javascript

 



   Subscribe in NewsGator Online    Subscribe in Rojo    Add to Google      Add to My AOL   Subscribe in FeedLounge   Add to netvibes  

Brendan Eich's Roadmap  Standard ECMA-262  www.schillmania.com  www.quirksmode.org 
www.snook.ca  www.davidflanagan.com  www.kryogenix.org  www.thomasfrank.se 
www.dustindiaz.com  Yahoo! UI Library  www.crockford.com  www.brainjacked.com 
chadlindstrom.ca  www.browserland.org     
       

Friday, July 14, 2006

 

Quick and Dirty Multiple Inheritence in Javascript


This has no checks for overriding properties so last object(arguement) wins:

/*function newObject() richard maloney 2006
Args: 2+ objects
Usage:send in n objects and it'll return one object inheriting
all the prop & methods of the objects passed in */
function object() {
var o=arguments,len=o.length-1,nextObj;
while(len--){
function f() {}
f.prototype =o[0], nextO=o[len+1];
for ( var i in nextO) { f.prototype[i]=nextO[i]; }
}
return new f();
}
You like? Here is some testing code:
  // setup the objects    
myFruitObj= {a:'apple', b:'banana'};
myFruitObj.peel=function(){this.b='banana peel';}
myFruitObj2={c:'carrot', d:'dill pickle'};
myFruitObj3={e:'eel', f:'fish', time:function(){alert('lets eat some'+ this.e)} };
// example call to our new function in this case
// existing myFruitObj3 is overridden
myFruitObj3=object(myFruitObj,myFruitObj2,myFruitObj3);

for ( var i in myFruitObj3) {
alert(i +" : "+ myFruitObj3[i])
}

Comments:
Greets to the webmaster of this wonderful site. Keep working. Thank you.
»
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

June 2006   July 2006   August 2006   September 2006  

This page is powered by Blogger. Isn't yours?