最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery check if var is a function and then call it - Stack Overflow

programmeradmin4浏览0评论

I have a variable name that I pass into a plugin, but the variable is actually a function. I use jquery $.isFunction to check if it is a function, and if it is, it should execute the function.

But I can't seem to make it work, I put some examples in jsfiddle:/

But here is a sample code:

HTML

<a class="one" href="#">click</a><br />
<a class="two" href="#">click</a><br />
<a class="three" href="#">click</a><br />

JS

$(document).ready(function() {

help = function(var1) {
    alert(var1);
}

function help2(var1) {
    alert(var1);
}


$('a.one').click(function() {
    var functionName = "help";
    if ($.isFunction([functionName])) {[functionName]("hello");
    } else {
        alert("not a function");
    }

    return false;
});

$('a.two').click(function() {
    var functionName = "help";

    if ($.isFunction(functionName)) {
        functionName("hello");
    } else {
        alert("not a function");
    }
    return false;
});

$('a.three').click(function() {
    var functionName = "help2";

    if ($.isFunction(functionName)) {
        functionName("hello");
    } else {
        alert("not a function");
    }
    return false;
});

$('a.four').click(function() {
    var functionName = "help2";

    if ($.isFunction([functionName])) {[functionName]("hello");
    } else {
        alert("not a function");
    }
    return false;
});

});​

As you can see, I tired a bunch of things, but all the wrong ones probably... I inspired some of them from: jQuery - use variable as function name

Overall I'm passing a variable that has the same name as a function, using jquery to check if it is a function, if it is, it should execute the function.

Thanks in advance for your help.

I have a variable name that I pass into a plugin, but the variable is actually a function. I use jquery $.isFunction to check if it is a function, and if it is, it should execute the function.

But I can't seem to make it work, I put some examples in jsfiddle:http://jsfiddle/tZ6U9/8/

But here is a sample code:

HTML

<a class="one" href="#">click</a><br />
<a class="two" href="#">click</a><br />
<a class="three" href="#">click</a><br />

JS

$(document).ready(function() {

help = function(var1) {
    alert(var1);
}

function help2(var1) {
    alert(var1);
}


$('a.one').click(function() {
    var functionName = "help";
    if ($.isFunction([functionName])) {[functionName]("hello");
    } else {
        alert("not a function");
    }

    return false;
});

$('a.two').click(function() {
    var functionName = "help";

    if ($.isFunction(functionName)) {
        functionName("hello");
    } else {
        alert("not a function");
    }
    return false;
});

$('a.three').click(function() {
    var functionName = "help2";

    if ($.isFunction(functionName)) {
        functionName("hello");
    } else {
        alert("not a function");
    }
    return false;
});

$('a.four').click(function() {
    var functionName = "help2";

    if ($.isFunction([functionName])) {[functionName]("hello");
    } else {
        alert("not a function");
    }
    return false;
});

});​

As you can see, I tired a bunch of things, but all the wrong ones probably... I inspired some of them from: jQuery - use variable as function name

Overall I'm passing a variable that has the same name as a function, using jquery to check if it is a function, if it is, it should execute the function.

Thanks in advance for your help.

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Sep 29, 2012 at 19:13 denislexicdenislexic 11.4k26 gold badges88 silver badges137 bronze badges 7
  • 4 [functionName] is always an array, not a function. And functionName is also not a function, it's a string. $.isFunction does not test whether a function with such a name exists, it really tests whether the value you pass to it is a function. The way you set up your code you cannot actually access those two functions by a string containing their names. – Felix Kling Commented Sep 29, 2012 at 19:17
  • Ok, that makes sense. Thanks (I do get confused with this). Is there any solution? – denislexic Commented Sep 29, 2012 at 19:19
  • Is this just a simplified example? Because here you could just call help and help2 directly. You mentioned a plugin, so whether a solution exist might depend on how the plugin works. – Felix Kling Commented Sep 29, 2012 at 19:21
  • @FelixKling - Let's hope that help = function... meant to be var help, because you can call it: jsfiddle/userdude/jSvPu Probably not remended... – Jared Farrish Commented Sep 29, 2012 at 19:23
  • 1 @JaredFarrish: Yeah, I assumed that actually. But even if not, help2 is local, so I am still at least 50% right ;) :D – Felix Kling Commented Sep 29, 2012 at 19:24
 |  Show 2 more ments

3 Answers 3

Reset to default 15

If you are wanting to call a function by a string of its name just use window.

var functionName = "help";
if ($.isFunction(window[functionName])) {
    window[functionName]("hello");
} else {
    alert("not a function");
}

You can use the following to invoke functions that are defined in the window/global scope, such as the function help:

if ($.isFunction(window[functionName])) {
  window[functionName]("hello");
}

help2, on the other hand, is not accessible this way since you are defining it in a closure. A possibile solution is to define the function outside of the .ready() handler. Then, you can use window[functionName] to call it:

var namespace = {
  help: function (var1) {
    alert(var1);
  },

  help2: function (var1) {
    alert(var1);
  }
}

$(document).ready(function() {
  var functionName = "help";
  if ($.isFunction(namespace[functionName])) {
    namespace[functionName]("hello");
  }
});

DEMO.

Check Fiddle for the working example.

Example have only one link working. make other links similarly.

Edit: after first ment

HTML

<a class="one" href="#">click</a><br />

JS

var help = function(var1) {
    alert(var1);
}
$(document).ready(function() {
    $('a.one').click(function() {
        var functionName = help;
        if ($.isFunction(functionName)) {
            functionName('test');
        } else {
            alert("not a function");
        }

        return false;
    });
});​
发布评论

评论列表(0)

  1. 暂无评论