15

関数内から別の関数に引数を転送するにはどうすればよいですか?

私はこれを持っています:

function SomeThing(options) {
  function callback(callback_name) {
    if(options[callback_name]) {
      // Desiring to call the callback with all arguments that this function
      // received, except for the first argument.
      options[callback_name].apply(this, arguments);
    }
  }
  callback('cb_one', 99, 100);
}

引数で得られるのaは「cb_one」ですが、「99」にしてほしいです。

var a = new SomeThing({
  cb_one: function(a,b,c) {
    console.log(arguments); // => ["cb_one", 99, 100]
    console.log(a);         // => cb_one
    console.log(b);         // => 99
    console.log(c);         // => 100
  }
});

どうすればいいですか?

4

2 に答える 2

24

使用するoptions[callback_name].apply(this, [].slice.call(arguments,1));

[].slice.call(arguments,1)引数 (' array-like ')を、最初の引数以外のすべての引数を含むObject実数に変換します。Array

[].slice.callと書くこともありますArray.prototype.slice.call

于 2013-07-07T07:22:59.960 に答える