I was trying to make my code smaller by caching functions to variables. For example:
function test(){
var a = Array.prototype.slice,
b = a.call(arguments);
// Do something
setTimeout(function(){
var c = a.call(arguments);
// Do something else
}, 200);
}
So instead of calling Array.prototype.slice.call(arguments), I can just do a.call(arguments);.
I was trying to make this even smaller by caching Array.prototype.slice.call, but that wasn’t working.
function test(){
var a = Array.prototype.slice.call,
b = a(arguments);
// Do something
setTimeout(function(){
var c = a(arguments);
// Do something else
}, 200);
}
This gives me TypeError: object is not a function. Why is that?
typeof Array.prototype.slice.call returns "function", like expected.
Why can’t I save .call to a variable (and then call it)?
Function.prototype.callis an ordinary function that operates on the function passed asthis.When you call
callfrom a variable,thisbecomeswindow, which is not a function.You need to write
call.call(slice, someArray, arg1, arg2)