简体   繁体   English

参数作为功能名称

[英]Parameter as function name

I am not sure if this is possible. 我不确定这是否可能。 I would like to pass in a function name as parameter like this 我想像这样传递函数名称作为参数

loadContent("http://test.com/", specialFunction);

specialFucntion is just a string : specialFucntion只是一个字符串:

function loadContent(href, functionIWant){
    $.ajax({
        type: "GET",
        url: href,
        dataType: "json",
        success: function(res, textStatus, xhr) {
            helper();
            functionIWant + "()"; //So this would be treated as function
                                  //and it actually calls specialFunction()
                                  //is it possible for this?
        }
    });
}

How do I achieve this? 我该如何实现?

ADD-ON 添加在

Let's say, I would pass in an array of function names, how do I call it? 假设,我要传入一个函数名数组,该怎么称呼它?

for(var i = 0; i < functionIWant.length; i++){
   functionIWant[i]; // how? appeciate a lot :)
}

You can just do it with functionIWant() 您可以使用functionIWant()

Example using your provided snippet: 使用您提供的代码段的示例:

function loadContent(href, functionIWant)
{
    $.ajax({
        type: "GET",
        url: href,
        dataType: "json",
        success: function(res, textStatus, xhr) {
            helper();
            functionIWant();
        }
    });
}

For your addendum 为了您的附录

Assuming the following three functions you want to call 假设您要调用以下三个函数

function foo() {alert("Foo")}
function bar() {alert("bar")}
function baz() {alert("baz")}

The best I can recommend if you're passed an array of function names as strings is to follow the advice here . 如果您将函数名称数组作为字符串传递给我,我建议的最好方法是遵循此处的建议。 Basically, you could just do it like this: 基本上,您可以这样做:

// Assuming FunctionIWant is an array of strings that represent function names
FunctionIWant = ["foo","bar","baz"];

...

for(var i=0;i<FunctionIWant.length;i++)
    window[FunctionIWant[i]]();

If, however, FunctionIWant is an array of actual functions, for example, you can simply iterate over them and call them individually like so: 但是,例如,如果FunctionIWant是实际函数的数组,则可以简单地对其进行迭代并分别调用它们,如下所示:

FunctionIWant = [foo,bar,baz] // note, these are not strings

for(var i=0;i<FunctionIWant.length;i++)
    FunctionIWant[i]();

In most cases, you'll be better off assigning the function rather than as a string 在大多数情况下,最好分配函数而不是将其分配为字符串

If you want to try to call the function represented by a parameter, simply call it as if that were the name of the function: 如果要尝试调用由参数表示的函数,只需像调用函数名称一样调用它即可:

function loadContent(href, callback)
{
    $.ajax({
        type: "GET",
        url: href,
        dataType: "json",
        success: function(res, textStatus, xhr) {
            helper();
            callback();
        }
    });
}

I am guessing that functionIWant is a string, not a reference to a function [You should make that clear when asking the question] 我猜functionIWant是一个字符串,而不是对函数的引用[问问题时应明确说明]

If that is the case, you want 如果是这样,您要

window[functionIWant]();

That will work if the function is in the global scope. 如果函数在全局范围内,那将起作用。 It would be better that you pass in the reference to the function or if you namespaced your functions. 最好将引用传递给函数,或者为函数命名空间。

var myFuncs = {
    foo : function(){ alert("asdf"); },
    bar : function(){ alert("qwerty"); }
};

var functionIWant = "foo";
myFuncs[functionIWant]();

for example you have two functions, 例如,您有两个功能,

var function1 = function( value ) {
  console.log( "foo: " + value );
};

var function2 = function( value ){
  console.log( "bar: " + value );
};

and in functionIWant array you have name of function, 在functionIWant数组中,您具有函数名称,

function loadContent(href, functionIWant)
{
   $.ajax({

    type: "GET",
    url: href,
    dataType: "json",
    success: function(res, textStatus, xhr) {
    helper();

    var callbacks = $.Callbacks();

    for(var i = 0; i < functionIWant.length; i++){
     callbacks.add( functionIWant[i] ); //on i=0 add function1
     callbacks.fire( "hello" );          // will fire function1
    }



      }
   });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM