2013-10-31 108 views
-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等等,id)。函數參數可以增長,但最後應該添加id作爲參數?

嘗試了很多解決方案,但沒有解決?檢查here

+0

PS我沒有發佈代碼,因爲它的巨大隻是張貼了我想做的事情,如果這就是爲什麼downvote是 – inputError

回答

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)` 
+0

arguments.length總是給0我試過它正在發佈它 – inputError

+0

@inputError你似乎混淆了'foo.length' ''foo'裏面的'arguments.length'。 –

+0

但我的問題是,當我分配opProcess進程。我還沒有調用過程函數。我想在調用它之前添加id來處理函數的參數 – inputError