我得到了一个字符串:

settings.functionName + '(' + t.parentNode.id + ')';

我想翻译成一个函数调用,像这样:

clickedOnItem(IdofParent);

当然,这必须用JavaScript来完成。当我在设置上做警报时。functionName + '(' + t.parentNode.id + ')';似乎每件事都是正确的。我只需要调用它转换成的函数。

传说:

settings.functionName = clickedOnItem

t.parentNode.id = IdofParent

当前回答

因为我讨厌eval,而且我不是一个人:

var fn = window[settings.functionName];
if(typeof fn === 'function') {
    fn(t.parentNode.id);
}

Edit: In reply to @Mahan's comment: In this particular case, settings.functionName would be "clickedOnItem". This would, at runtime translate var fn = window[settings.functionName]; into var fn = window["clickedOnItem"], which would obtain a reference to function clickedOnItem (nodeId) {}. Once we have a reference to a function inside a variable, we can call this function by "calling the variable", i.e. fn(t.parentNode.id), which equals clickedOnItem(t.parentNode.id), which was what the OP wanted.

更完整的例子:

/* Somewhere: */
window.settings = {
  /* [..] Other settings */
  functionName: 'clickedOnItem'
  /* , [..] More settings */
};

/* Later */
function clickedOnItem (nodeId) {
  /* Some cool event handling code here */
}

/* Even later */
var fn = window[settings.functionName]; 
/* note that settings.functionName could also be written
   as window.settings.functionName. In this case, we use the fact that window
   is the implied scope of global variables. */
if(typeof fn === 'function') {
    fn(t.parentNode.id);
}

其他回答

eval("javascript code");

它在处理JSON时被广泛使用。

因为我讨厌eval,而且我不是一个人:

var fn = window[settings.functionName];
if(typeof fn === 'function') {
    fn(t.parentNode.id);
}

Edit: In reply to @Mahan's comment: In this particular case, settings.functionName would be "clickedOnItem". This would, at runtime translate var fn = window[settings.functionName]; into var fn = window["clickedOnItem"], which would obtain a reference to function clickedOnItem (nodeId) {}. Once we have a reference to a function inside a variable, we can call this function by "calling the variable", i.e. fn(t.parentNode.id), which equals clickedOnItem(t.parentNode.id), which was what the OP wanted.

更完整的例子:

/* Somewhere: */
window.settings = {
  /* [..] Other settings */
  functionName: 'clickedOnItem'
  /* , [..] More settings */
};

/* Later */
function clickedOnItem (nodeId) {
  /* Some cool event handling code here */
}

/* Even later */
var fn = window[settings.functionName]; 
/* note that settings.functionName could also be written
   as window.settings.functionName. In this case, we use the fact that window
   is the implied scope of global variables. */
if(typeof fn === 'function') {
    fn(t.parentNode.id);
}

下面是一个更通用的方法来做同样的事情,同时支持作用域:

// Get function from string, with or without scopes (by Nicolas Gauthier)
window.getFunctionFromString = function(string)
{
    var scope = window;
    var scopeSplit = string.split('.');
    for (i = 0; i < scopeSplit.length - 1; i++)
    {
        scope = scope[scopeSplit[i]];

        if (scope == undefined) return;
    }

    return scope[scopeSplit[scopeSplit.length - 1]];
}

希望它能帮助一些人。

我花了一段时间才弄清楚,因为传统的窗口['someFunctionName']()一开始并不适合我。我的函数名是作为AJAX响应从数据库中提取的。此外,由于某种原因,我的函数是在窗口的作用域之外声明的,所以为了解决这个问题,我不得不重写我调用的函数

function someFunctionName() {}

to

window.someFunctionName = function() {}

从那里我可以调用window['someFunctionName']()轻松。我希望这能帮助到一些人!

我希望能够将函数名作为字符串,调用它,并将参数传递给函数。我无法得到这个问题的选择答案,但这个答案准确地解释了它,这里有一个简短的演示。

function test_function(argument)    {
    alert('This function ' + argument); 
}

functionName = 'test_function';

window[functionName]('works!');

这也适用于多个参数。