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])
}