在 Javascript 中的可变参数函数内调用可变参数函数?

发布于 2024-11-26 00:02:32 字数 249 浏览 3 评论 0原文

我有两个函数 a() 和 b(),都是可变参数函数,假设当我像这样调用函数 a() 时:

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

那么函数 b() 也会在 a() 内部被调用,但没有第一个参数a() 的参数列表中的“arg0”:

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

有什么办法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-12-03 00:02:32

每个 JavaScript function 实际上只是另一个“对象”(JavaScript 意义上的对象),并且带有一个 apply 方法(请参阅 Mozilla 文档)。因此你可以做这样的事情......

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文