admin管理员组文章数量:1278719
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)?
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)?
- which javascript engine gave you this error? – Dan D. Commented Dec 16, 2011 at 16:12
- possible duplicate of How do you reference Array.prototype.slice.call()? – pimvdb Commented Dec 16, 2011 at 16:13
3 Answers
Reset to default 7Function.prototype.call
is an ordinary function that operates on the function passed as this
.
When you call call
from a variable, this
bees window
, which is not a function.
You need to write call.call(slice, someArray, arg1, arg2)
Try this:
function test(){
var a = function(args){
return Array.prototype.slice.call(args);
};
b = a(arguments);
// Do something
setTimeout(function(){
var c = a(arguments);
// Do something else
}, 200);
}
The same error will happen if you try doing something like:
var log = console.log;
log("Hello");
The reason is that when you do this you are assigning the function x
(in my example log
) to the variable log
. BUT the function contains a call to this
which now refers to window
and not to console
, which then throws an error that this is not an object
The problem is that call
is a method (a function that belongs to an object) that expects its owner (its this
) to be a function. When you write a = Array.prototype.slice.call
, you're copying the function, but not the owner.
The "object is not a function" message isn't saying that a
isn't a function, it's saying that its this
isn't a function. You could technically achieve what you describe by writing a.call(Array.prototype.slice, arguments)
, but obviously that's not what you want!
本文标签: javascriptquotobject is not a functionquot when saving functioncall to a variableStack Overflow
版权声明:本文标题:javascript - "object is not a function" when saving function.call to a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262660a2367907.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论