4

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!

asked Nov 25, 2013 at 11:42
4
  • 3
    and what are you passing into callIt as a parameter? 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) Commented Nov 25, 2013 at 11:47
  • @MarkusOrreilly: Show the call to callIt, that's where the error is. Commented Nov 25, 2013 at 11:48
  • Make sure you are calling it as: callIt(MyFunction) and not callIt("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. Commented Nov 25, 2013 at 11:49

1 Answer 1

8

Your 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.

answered Nov 25, 2013 at 11:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.