I am working on a project that has a collection object that can create item objects. The item object also create and special handler object. When the user creates the collection object they can define the default handler object the item objects will generate and what arguments that handler can take.
I have found the task of moving and storing a reference to an Object prototype and subsequent constructor arguments a bit messy. This is what I have so far:
function Handler( val ) {
console.log( val );
}
function Collection( handlerArray ) {
this.handler = handlerArray;
}
Collection.prototype.addItem = function() {
new Item( this.handler );
};
function Item( handlerArray ) {
handlerRef = handlerArray.shift();
handlerArgs = handlerArray;
HandlerApply = function( args ) {
return handlerRef.apply( this, args );
};
HandlerApply.prototype = Handler.prototype;
new HandlerApply( handlerArgs );
}
myCollection = new Collection( [
Handler,
'Hello'
] );
myCollection.addItem();
Is there a more elegant solution to passing object prototypes and constructor arguments?
Thanks