在JS ES5中将数组作为函数参数传递

时间:2018-03-02 18:31:08

标签: javascript jstree

我正在为jQuery jstree plugin创建一个指令。

这是调用jstree函数的方式(例如:get_pathcheck_node):

get_path (obj, glue, ids)
check_node (obj)

在我的指令发布链接函数中,我传递了一些我想在jstree init中执行的动作,所以我传递了一个像这样的对象:

vm.initActions = {
    check_node: [nodesToCheck],
    get_path: [obj, glue, ids]
};

然后执行这些功能:

if(scope.initActions){
    Object.keys(scope.initActions).forEach(function(action){
        if(scope.treeView.jstree(true).hasOwnProperty(action)){
            scope.treeView.jstree(true)[action].apply(this, scope.initActions[action]);
        }
    });
}

正如您所看到的,我正在尝试将数组作为函数参数传递,因此它将是这样的:

scope.treeView.jstree(true).check_node(nodesToCheck);
scope.treeView.jstree(true).get_path(obj, glue, ids);

但这不起作用我收到了这个错误:

  

TypeError:无法读取未定义

的属性“设置”

如果错误(jstree.js):

,这就是源码
this.check_node = function (obj, e) {
            if(this.settings.checkbox.tie_selection)

另一方面,点差运算符工作正常:

if(scope.initActions){
    Object.keys(scope.initActions).forEach(function(action){
        if(scope.treeView.jstree(true).hasOwnProperty(action)){
            scope.treeView.jstree(true)[action](...scope.initActions[action]);
        }
    });
}

不幸的是,我不能使用扩展运算符,因为我使用ES5。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用该功能的apply方法:

var context = scope.treeView.jstree(true);
context[action].apply(context, scope.initActions[action]);
相关问题