-1
// set Process
i18n.setProcess(function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff') });

// Setter and getter 
this.setProcess = function( opProcess ) {  //here param opProcess is function with parameters see i18n.setProcess() line of code
        if( opProcess == undefined) 
            throw "Process is undefined";
        if( $.isFunction(opProcess) == false )
            throw "Process is not a function"
             process = opProcess;

    };
    this.getProcess = function() {
        return process;
    };

了解 i18n.setProcess 如何将带有 param 作为参数的函数传递给 setProcess。

现在我在 SetProcess 中想要的是function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff',**id**)// id 被动态添加到函数本身,它作为参数传递给 setProcess

问题 - 我想在设置过程中动态添加 id(在我的类变量中定义,总是可以通过 id 访问)并添加函数参数(Url,etc,etc,id)。函数参数可以增长,但 id 应该最后添加为参数?

尝试了很多解决方案但没有成功?在这里检查

4

1 回答 1

2

这就是arguments对象的用途..

function foo() {
    var lastArg = arguments.length ? arguments[arguments.length - 1] : 'default';
    return lastArg;
}

foo();     // "default"
foo(1);    // 1
foo(1, 2); // 2

如果你想编写一个类似于bind只在最后粘贴参数的函数,那么你可以这样做

function appendArguments(fn) {
    var slice = Array.prototype.slice.call.bind(Array.prototype.slice),
        args = slice(arguments, 1);
    return function () {
        return fn.apply(this, slice(arguments).concat(args));
    };
}

现在

var bar = appendArguments(foo, 3);
bar();     // 3
bar(4, 5); // 3 (because it's calling `foo(4, 5, 3)`
于 2013-10-31T15:14:26.537 回答