JavaScript函数可以接受最大数量的参数吗? [英] Is there a max number of arguments JavaScript functions can accept?

查看:93
本文介绍了JavaScript函数可以接受最大数量的参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JavaScript函数可以接受任意数量的参数。

I know that JavaScript functions can accept "any" number of arguments.

function f(){};
f(1,2,3,4 /*...*/);

但我想知道实际上有多少任何可以限制?

But I'm wondering if there is actually a limit to how many "any" can be?

例如,假设我将一百万个参数交给 f()。那会有用吗?或者翻译龙骨会过来吗?

E.g., let's say I hand a million arguments to f(). Would that work? Or would the interpreter keel over?

我猜测最大值是(a)特定于实现或(b)(2 ^ 32 )-1 ,因为参数对象是类似数组的。

I'm guessing the maximum is either (a) implementation-specific or (b) (2^32)-1, since the arguments object is array-like.

我不喜欢在语言规范中看到这一点,但我可能没有连接一些点。

I don't see this mentioned in the language specification, but I might not be connecting some dots.

推荐答案

虽然没有具体的限制规范中理论最大参数数量(如 thefortheye 的答案指出)。当然有实用限制。这些限制完全取决于实现,并且很可能也完全取决于您正在调用该函数的

Although there is nothing specific limiting the theoretical maximum number of arguments in the spec (as thefortheye's answer points out). There are of course practical limits. These limits are entirely implementation dependent and most likely, will also depend exactly on how you're calling the function.

我创建了这个小提琴作为一个实验。

I created this fiddle as an experiment.

function testArgs() {
    console.log(arguments.length);
}

var argLen = 0;
for (var i = 1; i < 32; i++) {
    argLen = (argLen << 1) + 1;
    testArgs.apply(null, new Array(argLen));
}

以下是我的结果:


  • Chrome 33.0.1750.154 m:上一次成功的测试是 65535 参数。之后失败了:


未捕获RangeError:超出最大调用堆栈大小

Uncaught RangeError: Maximum call stack size exceeded


  • Firefox 27.0.1:最后一次成功的测试是 262143 参数。之后失败了:


    RangeError:传递给Function.prototype.apply的arguments数组太大

    RangeError: arguments array passed to Function.prototype.apply is too large


  • Internet Explorer 11:上一次成功的测试是 131071 参数。之后它失败了:

  • Internet Explorer 11: The last successful test was 131071 arguments. After that it failed with:


    RangeError:SCRIPT28:堆栈空间不足

    RangeError: SCRIPT28: Out of stack space


  • Opera 12.17:最后一次成功的测试是 1048576 参数。之后失败了:


    错误:Function.prototype.apply:argArray太大

    Error: Function.prototype.apply: argArray is too large


  • 当然,这里可能还有其他因素可能会有不同的结果。

    Of course, there may be other factors at play here and you may have different results.

    这是替代小提琴使用 eval 创建。同样,您可能会得到不同的结果。

    And here is an alternate fiddle created using eval. Again, you may get different results.


    • Chrome 33.0.1750.154 m:上一次成功的测试 32767 论点。之后它失败了:

    • Chrome 33.0.1750.154 m: The last successful test was 32767 arguments. After that it failed with:


    未捕获的SyntaxError:函数调用中的参数太多(仅允许32766)

    Uncaught SyntaxError: Too many arguments in function call (only 32766 allowed)

    这个特别有趣,因为Chrome本身似乎对实际允许的参数数量感到困惑。

    Firefox 27.0.1:上一次成功的测试是 32767 参数。之后它失败了:

    Firefox 27.0.1: The last successful test was 32767 arguments. After that it failed with:


    脚本太大

    script too large


  • Internet Explorer 11:上一次成功的测试是 32767 参数。之后它失败了:

  • Internet Explorer 11: The last successful test was 32767 arguments. After that it failed with:


    RangeError:SCRIPT7:内存不足

    RangeError: SCRIPT7: Out of memory


  • Opera 12.17:最后一次成功的测试是 4194303 参数。之后它失败了:

  • Opera 12.17: The last successful test was 4194303 arguments. After that it failed with:


    内存不足;脚本终止。

    Out of memory; script terminated.


  • 这篇关于JavaScript函数可以接受最大数量的参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆