0

我想在浏览器中加载并运行它时创建我的函数 javascript 的名称。

我想要这个 php 代码的等价性:

<?php

$name_function = 'toto';

function toto() {
    echo 'toto';
}


$name_function();

?>

我的评估:邪恶:)

var chaine = 'TabToto';
var store = chaine.substr(3,chaine.length);
eval ('store = this.get'+store+'Store()');

好的,我想要什么:

var chaine = 'TabToto';
var store = 'get'+chaine.substr(3,chaine.length)+'Store';

store = this[store]();

我的控制器创建了一个获取商店的功能,我想使用它...

4

3 回答 3

4
function toto()
{
    alert('toto');
}

var name_function  = 'toto';
window[name_function]();

(假设这是在全球范围内)

于 2013-06-09T19:55:48.783 回答
3

除了将函数名称作为字符串提供之外,您还可以将函数本身的引用分配给变量。

看起来像这样:

function toto() {
    alert('toto');
}
function toto2() {
    alert('toto 2');
}

var function_to_use = toto;   // notice the lack of quotes
function_to_use();

function_to_use = toto2;
function_to_use();
于 2013-06-09T20:14:52.740 回答
0

我已经这样做了

function a(){
alert(1);

}
var aa=a;

 aa();
于 2013-06-09T22:43:43.813 回答