Why does apply with too many arguments throw “Maximum call stack size exceeded”?

删除回忆录丶 提交于 2020-01-14 07:50:07

问题


In Chrome and Node, the following code throws an error:

function noop() {}
var a = new Array(1e6)
// Array[1000000]
noop.apply(null, a)
// Uncaught RangeError: Maximum call stack size exceeded

I understand why it might be a Bad Idea to pass 1 million arguments to a function, but can anyone explain why the error is Maximum call stack size exceeded instead of something more relevant?

(In case this seems frivolous, the original case was Math.max.apply(Math, lotsOfNumbers), which is a not-unreasonable way of getting the max number from an array.)


回答1:


Function arguments are put on the stack. You're trying to put a million arguments onto the stack, and that's more than the maximum stack size. So the error message is very relevant to the reason for the error.



来源:https://stackoverflow.com/questions/42263108/why-does-apply-with-too-many-arguments-throw-maximum-call-stack-size-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!