Javascript 传递名称为变量的函数引用

Javascript 传递名称为变量的函数引用,javascript,Javascript,我有以下javascript方法: function 123_test_function(){ } 该函数由java生成并发送到客户端。123是组件的id,因此它可以更改。i、 e我可以使用另一个名为111\u test\u function() 我想将此函数作为引用传递 所以我需要创建引用 var 123_test_function = function 123_test_function(){ } 在对象内的另一个js文件中,我有一个函数需要使用123_test_函数引用,如下所示:

我有以下javascript方法:

function 123_test_function(){

}
该函数由java生成并发送到客户端。123是组件的id,因此它可以更改。i、 e我可以使用另一个名为
111\u test\u function()
我想将此函数作为引用传递

所以我需要创建引用

var 123_test_function = function 123_test_function(){

}
在对象内的另一个js文件中,我有一个函数需要使用123_test_函数引用,如下所示:

useFunction(123_test_function);
我遇到的问题是函数的
123
部分。 在这个对象中,我有一个变量(
uniqueID
),它的数字位于函数的开头。 我需要函数调用类似于:

useFunction(uniqueID+"_test_function");
这似乎不是传递函数,而是传递字符串。
我做错了什么吗?

如果函数被定义为全局函数,它将是全局对象的成员(
窗口,如果是浏览器)。因此,您只需执行
window['id\u'+uniqueID+''u test\u function']
即可访问您的函数

useFunction(window['id_'+uniqueID+'_test_function'])

(在JavaScript中,标识符不能以数字开头,因此我添加了
'id.
前缀。当然,您可以根据自己的喜好对其进行更改。)

如果函数定义为全局函数,它将是全局对象的一个成员(
对于浏览器,则为
窗口
)。因此,您只需执行
window['id\u'+uniqueID+''u test\u function']
即可访问您的函数

useFunction(window['id_'+uniqueID+'_test_function'])
(在JavaScript中,标识符不能以数字开头,因此我添加了
'id.
前缀。当然,您可以根据自己的喜好对其进行更改。)

像这样调用函数

var func = test_function(1)
func();
像这样调用函数

var func = test_function(1)
func();
首先,标识符(如函数名)不能以数字开头

要解决问题,请使用以下对象:

// 1. define an object to hold all your functions
var allFunctions = {};

// 2. store any function with a unique string as the ID
allFunctions['123_test_function'] = function () {
  // whatever
};

// 3. call the function
allFunctions['123_test_function']();
allFunctions[uniqueID + '_test_function']();
// Do this once at the top of your JS
var test_functions = [];

// Now, for each function you define:
test_functions[123] = function() {
  // Do stuff here
};

// And when you need to call the functions:
var funcId = 123;
test_functions[funcId]();
对象是关联数组。它们存储键/值对,因此它们可以完全满足您的需要

请注意,函数在JavaScript中不需要名称,因此我在步骤2中没有使用on。

首先,标识符(如函数名)不能以数字开头

要解决问题,请使用以下对象:

// 1. define an object to hold all your functions
var allFunctions = {};

// 2. store any function with a unique string as the ID
allFunctions['123_test_function'] = function () {
  // whatever
};

// 3. call the function
allFunctions['123_test_function']();
allFunctions[uniqueID + '_test_function']();
// Do this once at the top of your JS
var test_functions = [];

// Now, for each function you define:
test_functions[123] = function() {
  // Do stuff here
};

// And when you need to call the functions:
var funcId = 123;
test_functions[funcId]();
对象是关联数组。它们存储键/值对,因此它们可以完全满足您的需要


请注意,函数在JavaScript中不需要名称,因此我在第2步中没有使用on。

正如一些人正确指出的那样,函数(或变量)名称不能以数字开头。这种语法也是错误的:

var 123_test_function = function 123_test_function(){
} 
正确的语法是:

var 123_test_function = function() {
};
…虽然还应注意,这与“传统”的效果完全相同

…声明,
窗口
对象
的上下文中-由于
窗口
实际上是浏览器中JS环境的全局范围,因此无论您如何定义函数,它们始终可以从任何地方访问。准确理解Javascript中声明函数的每个方法的含义非常重要-幸运的是,再次

人们提出了从字符串上下文调用命名函数的各种方法,基本上是尝试使用“variable”语法,这是一个已经详细讨论过的主题。只要可能,就应该避免使用
eval()
方法-如果您发现自己需要
eval()
,那么您不久前可能在某个地方出错了@Tomalak对一个对象中的函数集合有正确的想法,但这仍然需要使用稍微凌乱的字符串方法来引用实际由数字ID访问的内容。收集方法的优点是不会将
窗口
对象与可能是单次/零次使用的成员混淆

但在我看来,这里实际上只需要一个函数的索引数组,其中只需要数字索引就可以访问它们。我建议您创建如下函数:

// 1. define an object to hold all your functions
var allFunctions = {};

// 2. store any function with a unique string as the ID
allFunctions['123_test_function'] = function () {
  // whatever
};

// 3. call the function
allFunctions['123_test_function']();
allFunctions[uniqueID + '_test_function']();
// Do this once at the top of your JS
var test_functions = [];

// Now, for each function you define:
test_functions[123] = function() {
  // Do stuff here
};

// And when you need to call the functions:
var funcId = 123;
test_functions[funcId]();

正如一些人正确指出的那样,函数(或变量)名称不能以数字开头。这种语法也是错误的:

var 123_test_function = function 123_test_function(){
} 
正确的语法是:

var 123_test_function = function() {
};
…虽然还应注意,这与“传统”的效果完全相同

…声明,
窗口
对象
的上下文中-由于
窗口
实际上是浏览器中JS环境的全局范围,因此无论您如何定义函数,它们始终可以从任何地方访问。准确理解Javascript中声明函数的每个方法的含义非常重要-幸运的是,再次

人们提出了从字符串上下文调用命名函数的各种方法,基本上是尝试使用“variable”语法,这是一个已经详细讨论过的主题。只要可能,就应该避免使用
eval()
方法-如果您发现自己需要
eval()
,那么您不久前可能在某个地方出错了@Tomalak对一个对象中的函数集合有正确的想法,但这仍然需要使用稍微凌乱的字符串方法来引用实际由数字ID访问的内容。收集方法的优点是不会将
窗口
对象与可能是单次/零次使用的成员混淆

但在我看来,这里实际上只需要一个函数的索引数组,其中只需要数字索引就可以访问它们。我建议您创建如下函数:

// 1. define an object to hold all your functions
var allFunctions = {};

// 2. store any function with a unique string as the ID
allFunctions['123_test_function'] = function () {
  // whatever
};

// 3. call the function
allFunctions['123_test_function']();
allFunctions[uniqueID + '_test_function']();
// Do this once at the top of your JS
var test_functions = [];

// Now, for each function you define:
test_functions[123] = function() {
  // Do stuff here
};

// And when you need to call the functions:
var funcId = 123;
test_functions[funcId]();

啊,对不起,我在这篇文章中用123作为例子,但变量实际上不是一个数字。啊,对不起,我在这篇文章中用123作为例子,但变量实际上不是一个数字。关于这个问题,这是正确的答案!此外,此示例在函数名包含变量或函数名之前正确地预加字符串,该函数名不应以与qu相关的numberIn开头