0

I checked out this reference link and ended up using Matthew's solution, as it works for me.

var factory ={};
factory.Tree = function(arg1,arg2,arg3,arg4){
    console.log(arg1+""+arg2+""+arg3+""+arg4); 
}
function instantiate(classname){
    return new (function(a){ return factory[classname].apply(this,a);})(Array.prototype.splice.call(arguments,1));
    // also is this ^^^ a good practice? instead of declaring the temp function beforehand
    // function t(a) {return factory[classname].apply(this,a);}
    // return new t(Array.prototype.splice.call(arguments,1));
}
var newObj = instantiate("Tree",1,2,3,4); // this prints 1234 --> works

Though, I'm not sure why using user123444555621's solution only works if I pass in "arguments" (that is everything including "classname"):

function instantiate(classname){
    return new (Function.prototype.bind.apply(factory[classname], arguments));
}
var newObj = instantiate("Tree",1,2,3,4); // this prints 1234 --> works

but if I slice "arguments" and remove "classname", then pass in the result array, it does not work as expected:

function instantiate(classname){
    var args = Array.prototype.splice.call(arguments,1); 
        // ^^ I checked and this prints 1,2,3,4 as well
    return new (Function.prototype.bind.apply(factory[classname], args));
}
var newObj = instantiate("Tree",1,2,3,4); // this prints 234undefined

I'm not sure why but somehow it seems like the args array is sliced (again) and removes its first element (1 in this case).

Could someone offer any insights? Thanks

2 Answers 2

1

Did you use the right array function slice vs splice ?

Array.prototype.slice() - Creates a new array from elements of an existing array. It does not modify the original array.

Array.prototype.splice() – Deletes and/or inserts elements in an array. Unlike slice(), the splice() method modifies the original array and returns a new array. The splice() method takes three arguments.

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is this line

var args = Array.prototype.splice.call(arguments,1); 

You have essentially removed the first item from the arguments while turning it into array args

simply replace 1 by 0

var args = Array.prototype.splice.call(arguments,0);

DEMO

var factory ={};
factory.Tree = function(arg1,arg2,arg3,arg4){
    console.log(arg1+""+arg2+""+arg3+""+arg4); 
}
function instantiate(classname){
    var args = Array.prototype.splice.call(arguments,0); 
    return new (Function.prototype.bind.apply(factory[classname], args));
}
instantiate("Tree",1,2,3,4);

1 Comment

Thanks. Correct me if I'm wrong but aren't we supposed to remove the first argument and return the remaining as an array? In this case, remove "Tree", and pass in [1,2,3,4] to the constructor of Tree? If not removing anything from arguments, then maybe I could just pass it directly to apply() (just not sure why it works)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.