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!

Community
  • 1
  • 1
  • 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")`. – the system Feb 12 '13 at 15:27
  • Your edit disappeared. I wonder why there's no revision history showing up. – the system Feb 12 '13 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. – the system Feb 12 '13 at 15:46

2 Answers2

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');
the system
  • 9,244
  • 40
  • 46
  • Nope, that's not the problem here. Have a look at the other answer. – Cerbrus Feb 12 '13 at 15:21
  • @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`. – the system Feb 12 '13 at 15:23
  • 1
    @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. – the system Feb 12 '13 at 15:25
  • @JoeCoderGuy: to make it global, declare it outside any other function, or explicitly assign it to `window`, like `window.callThisPlease = function() { ... };` – the system Feb 12 '13 at 15:43
  • 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 – the system Feb 12 '13 at 15:52
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);
    });
});
Richard
  • 8,110
  • 3
  • 36
  • 59