在JS中动态创建函数

use*_*855 27 javascript

我正在为JS游戏创建AI引擎,它由有限状态机构成.我正在从XML加载状态数和它们的变量值.我也想加载行为,因为我没有时间创建脚本语言,我认为将JS代码"插入"外部文件(在XML节点内)并在上面执行它是个好主意.需求.

这样的事情

<evilguy1>
    <behaviour>
        this.x++;
    </behaviour>
    <behaviour>
        this.y++;
    </behaviour>
</evilguy1>
Run Code Online (Sandbox Code Playgroud)

对于这样的事情:

function behaviour_1(){
    this.x++;
}
function behaviour_2(){
    this.y++;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,现在我已经加载了代码,我该如何执行它?我想为每个代码'node'创建一个具有唯一名称的函数,然后从游戏逻辑中调用它们,但我不知道这是否可行(因为你可以从HTML加载更多的JS代码,你也应该能够从JS代码中做到这一点,没有?).如果没有,有没有类似的解决方案?提前致谢!

(PS:外部库依赖性越小越好)

编辑1:

好的,现在我知道如何创建包含代码的函数

window[classname] = function() { ... };
Run Code Online (Sandbox Code Playgroud)

小智 69

好吧,你可以使用Function构造函数,就像在这个例子中一样:

var f = new Function('name', 'return alert("hello, " + name + "!");');
f('erick');
Run Code Online (Sandbox Code Playgroud)

这样你就可以用参数和body定义一个新函数,并将它赋给变量f.您可以使用哈希集并存储许多函数:

var fs = [];
fs['f1'] = new Function('name', 'return alert("hello, " + name + "!");');
fs['f1']('erick');
Run Code Online (Sandbox Code Playgroud)

加载xml取决于它是在浏览器还是服务器上运行.


Met*_*win 6

扩展Ericks关于Function构造函数的答案.

Function构造函数创建一个匿名函数,在运行时错误将为调用堆栈中的每个函数(使用Function创建)打印出匿名函数.这可能会使调试变得更难.

通过使用实用程序功能,您可以动态命名所创建的功能并绕过这种困境.此示例还将函数数组内的每个函数的所有主体合并为一个,然后将所有函数作为一个命名函数返回.

const _createFn = function(name, functions, strict=false) {

    var cr = `\n`, a = [ 'return function ' + name + '(p) {' ];

    for(var i=0, j=functions.length; i<j; i++) {
        var str = functions[i].toString();
        var s = str.indexOf(cr) + 1;
        a.push(str.substr(s, str.lastIndexOf(cr) - s));
    }
    if(strict == true) {
        a.splice(1, 0, '\"use strict\";' + cr)
    }
    return new Function(a.join(cr) + cr + '}')();
}
Run Code Online (Sandbox Code Playgroud)

关于Function构造函数的问题:

函数表达式定义的函数继承当前范围.也就是说,该函数形成一个闭包.另一方面,由Function构造函数定义的函数不会继承除全局范围(所有函数都继承)之外的任何范围.

来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#Differences


Mat*_*ves 5

假设您有一个节点名称数组和一个函数体的并行数组:

var functions = {};
var behaviorsNames = ['behavior1', 'beahvior2'];
var behaviorsBodies = ['this.x++', 'this.y++'];
for (var i = 0; i < behaviorsNames.length; i++){
    functions[behaviorsNames[i]] =  new Function(behaviorsBodies[i]);
}

//run a function
functions.behavior1();
Run Code Online (Sandbox Code Playgroud)

或作为全局变量:

var behaviorsNames = ['behavior1', 'beahvior2'];
var behaviorsBodies = ['this.x++', 'this.y++'];
for (var i = 0; i < behaviors.length; i++){
    window[behaviors[i]] = new Function(behaviorsBodies[i]);
}
Run Code Online (Sandbox Code Playgroud)