function sumArguments () {
var value = this instanceof Number ? this : 0;
for (var i = 0; i < arguments.length; i++) {
value += arguments[i];
}
return value;
}
Function.prototype.apply1 = function() {
var fn = this;
var args = Array.prototype.slice.call(arguments);
var context = args.shift();
var newArgs = args[0];
var boundfn = fn.bind(context, newArgs);
return boundfn();
}
console.log(sumArguments.apply1(4, [1, 2, 3])); --> should equal 10
How can one implement apply without using apply? I'm getting stuck on how to transform the array of [1,2,3] to be passed in as 1,2,3 to bind.
eval()after constructing a string of code, it would likely require a native extension of the engine itself. The nativeCall()function and[[Call]]methods that define a function's ability to be invoked behave similarly to.apply(), accepting anargumentsList. Spreading the arguments is actually counter-productive.