1

Passing function name as a parameter to another function doesn't seem to work for me.

I've tried every variation from every article I can find. Currently, I have this in one js file:

function callThisPlease (testIt){
    alert(testIt);
}

$(document).ready(function () {
    $.fn.pleaseCallTheOtherFunction('callThisPlease');
});

I have this in another:

$(document).ready(function () {

    $.fn.pleaseCallTheOtherFunction = function(functionName){
        window[functionName].apply('works');
    }

});

chrome console says Uncaught TypeError: Cannot call method 'apply' of undefined.

Please help. Many thanks in advance!

3
  • 2
    Re your edit: You're now passing the function itself. If the function name is known ahead of time, then you don't need to pass a string to window[...]. Just do functionName("works"). Commented Feb 12, 2013 at 15:27
  • Your edit disappeared. I wonder why there's no revision history showing up. Commented Feb 12, 2013 at 15:32
  • The information you add seems to contradict previous information. I think you need to step back and describe in detail what you need. Commented Feb 12, 2013 at 15:46

2 Answers 2

3

If the method is undefined on window, that means your function isn't global. Make it a global function.


Also, you can get rid of .apply. Currently you're passing 'works' as the this value.

window[functionName]('works');
Sign up to request clarification or add additional context in comments.

5 Comments

Nope, that's not the problem here. Have a look at the other answer.
@Cerbrus: It is the problem if OP wants to invoke the function by a string name... like if the name is coming dynamically from some external source. The other answer won't work because the function will be used as a property name of window.
@Cerbrus: To be fair, the question is a little unclear. It's tough to tell what is ultimately needed. If the function name is to be hard coded, then I'd agree that the function itself should be passed instead of a string.
@JoeCoderGuy: to make it global, declare it outside any other function, or explicitly assign it to window, like window.callThisPlease = function() { ... };
You seem to be taking bits and pieces from both answers and trying to combine them. The two answers here are very different because we can't tell what you ultimately need. In JavaScript, you can pass strings, and you can pass functions. If the code doing the passing has direct access to the function, then you should pass the function and invoke it directly. If the code doing the passing doesn't have direct access to the functoin, or doesn't know ahead of time what the function name will be, then you pass a string and use that string to access a property on window where the function is stored
2

jsFiddle Demo

Setup

Firstly you'll need to setup the pleaseCallTheOtherFunction method, like so:

$.fn.pleaseCallTheOtherFunction = function(otherFunction) {
    if ($.isFunction(otherFunction)) {
        otherFunction.apply(this, ['works']);
    }
};

Usage

Then you'll want to create your 'replace' function (delegate), and then call it without quotes, like so:

function callThisPlease (testIt){
    alert(testIt);
}

$(document).ready(function () {
    $().pleaseCallTheOtherFunction(callThisPlease);
});

Alternatively

You could write an in-line function:

$(document).ready(function () {
    $().pleaseCallTheOtherFunction(function(testIt) {
        alert(testIt);
    });
});

3 Comments

Did you see OP's pleaseCallTheOtherFunction? It requires a function name as string, as parameter.
Try passing the function without the quotes, so you pass the actual function, rather than the function name.
I've added a JsFiddle as an example; hopefully this should help a little bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.