最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling a variadic function inside a variadic function in Javascript? - Stack Overflow

programmeradmin1浏览0评论

I have two function a() and b(), both are variadic functions, let say when I call function a() like this :

a(arg0, arg1, arg2, arg3, ...., argn);

then the function b() will be called as well inside a(), but without the first argument "arg0" in the arguments list of a() :

b(arg1, arg2, arg3, ...., argn);

Is there any way for it?

I have two function a() and b(), both are variadic functions, let say when I call function a() like this :

a(arg0, arg1, arg2, arg3, ...., argn);

then the function b() will be called as well inside a(), but without the first argument "arg0" in the arguments list of a() :

b(arg1, arg2, arg3, ...., argn);

Is there any way for it?

Share Improve this question asked Jul 24, 2011 at 18:01 TeivTeiv 2,63510 gold badges39 silver badges49 bronze badges 1
  • 1 Related: stackoverflow.com/questions/960866/… – Ray Toal Commented Jul 24, 2011 at 18:17
Add a comment  | 

1 Answer 1

Reset to default 25

Every JavaScript function is really just another "object" (object in the JavaScript sense), and comes with an apply method (see Mozilla's documentation). You can thus do something like this....

b = function(some, parameter, list) { ... }

a = function(some, longer, parameter, list)
{
   // ... Do some work...

   // Convert the arguments object into an array, throwing away the first element
   var args = Array.prototype.slice.call(arguments, 1);

   // Call b with the remaining arguments and current "this"
   b.apply(this, args);
}
发布评论

评论列表(0)

  1. 暂无评论