admin管理员组文章数量:1279044
This code is giving me a SCRIPT5002: Function expected
error:
var callIt = function(func) { func(); }
WHY!? It's like it's trying to do type checking or something
EDIT: use case
var callIt = function(func) { func(); }
function nextSlide() {
var fn = currSlide ? currSlide.hide : callIt;
currSlide = setupSlides[++slideIdx];
fn(currSlide.show());
}
DOH!
This code is giving me a SCRIPT5002: Function expected
error:
var callIt = function(func) { func(); }
WHY!? It's like it's trying to do type checking or something
EDIT: use case
var callIt = function(func) { func(); }
function nextSlide() {
var fn = currSlide ? currSlide.hide : callIt;
currSlide = setupSlides[++slideIdx];
fn(currSlide.show());
}
DOH!
Share Improve this question edited Nov 25, 2013 at 11:49 d0c_s4vage asked Nov 25, 2013 at 11:42 d0c_s4vaged0c_s4vage 4,0776 gold badges27 silver badges32 bronze badges 4-
3
and what are you passing into
callIt
as a parameter? – Mark Walters Commented Nov 25, 2013 at 11:46 -
a function.
callIt
is to serve as a tmp function to replace a jquery animation function. Eg:(currentSlide ? currentSlide.hide : callIt)(nextSlide.fadeIn)
– d0c_s4vage Commented Nov 25, 2013 at 11:47 -
@MarkusOrreilly: Show the call to
callIt
, that's where the error is. – T.J. Crowder Commented Nov 25, 2013 at 11:48 -
Make sure you are calling it as:
callIt(MyFunction)
and notcallIt("myFunction")
Also, for clarity, make sure to end your statement in a semicolon so an anonymous function afterwards doesn't attempt to call the function. – Ultimater Commented Nov 25, 2013 at 11:49
1 Answer
Reset to default 8Your code:
fn(currSlide.show());
...calls currSlide.show()
and passes the return value from calling it into fn
, exactly the way foo(bar())
calls bar
and passes its return value into foo
.
since the return value of show
is not a function, you get the error. You may have meant:
fn(function() { currSlide.show(); });
Note, though, that you have a problem here:
var fn = currSlide ? currSlide.hide : callIt;
If currSlide
is truthy, you'll get a reference to the hide
function, but that function is not in any way connected to currSlide
. If you call it later, it's likely to fail because it's expecting this
to mean something specific.
If you can rely on having the features from ECMAScript5 (so, you're using a modern browser other than IE8 and/or you're including an "es5 shim", you can fix that with Function#bind
:
var fn = currSlide ? currSlide.hide.bind(currSlide) : callIt;
Or if you're using jQuery, you can fix it with jQuery's $.proxy
:
var fn = currSlide ? $.proxy(currSlide.hide, currSlide) : callIt;
Both of those return a new function that, when called, will call the target function with the given this
value.
If you're not using ES5 or jQuery, well, this would do it:
var prevSlide = currSlide;
var fn = prevSlide ? function(func) { prevSlide.hide(func); } : callIt;
...but at that point I suspect stepping back and reevaluating might be in order.
本文标签: IE10 Javascript quotFunction ExpectedquotStack Overflow
版权声明:本文标题:IE10 Javascript "Function Expected" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741263726a2368076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论