我可以为函数创建动态名称吗?例如:
var name = 'test1';
function [name]() { ..... };现在我可以调用test1();这个函数名是{.};但是名称为test1,意味着函数将运行为
function test1() { ..... };如果我更改
var name = 'test2';我可以调用test2();这个函数名是{.};但是名称为test2,意味着函数将运行为
function test2() { ..... };那能办到吗?
发布于 2014-10-17 02:31:06
var test1 = function() {...};
var test2 = test1;
// now both of the following work
test1();
test2();如果您在web浏览器中工作,还可以将此函数分配给全局对象window中的字符串值。就像这样
var originalFunction = function() {....};
var newName = "foo";
window[newName] = originalFunction;
// now both of the following work.
foo();
originalFunction();https://stackoverflow.com/questions/26416973
复制相似问题