admin管理员组文章数量:1406937
I got a nice solution to get HTML Comments from the HTML Node Tree
var findComments = function(el) {
var arr = [];
for (var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
if (node.nodeType === 8) {
arr.push(node);
} else {
arr.push.apply(arr, findComments(node));
}
}
return arr;
};
var mentNodes = findComments(document);
// whatever you were going to do with the ment...
console.log(mentNodes[0].nodeValue);
from this thread. Everything I did was adding this small loop to print out all the nodes.
var arr = [];
var findComments = function(el) {
for (var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
if (node.nodeType === 8) {
arr.push(node);
} else {
arr.push.apply(arr, findComments(node));
}
}
return arr;
};
var mentNodes = findComments(document);
//I added this
for (var counter = arr.length; counter > 0; counter--) {
console.log(mentNodes[counter].nodeValue);
}
I keep getting this Error Message:
RangeError: too many arguments provided for a function call debugger eval code:9:13
EDIT: i had a typo while pasting changed the code from i-- to counter--
I got a nice solution to get HTML Comments from the HTML Node Tree
var findComments = function(el) {
var arr = [];
for (var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
if (node.nodeType === 8) {
arr.push(node);
} else {
arr.push.apply(arr, findComments(node));
}
}
return arr;
};
var mentNodes = findComments(document);
// whatever you were going to do with the ment...
console.log(mentNodes[0].nodeValue);
from this thread. Everything I did was adding this small loop to print out all the nodes.
var arr = [];
var findComments = function(el) {
for (var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
if (node.nodeType === 8) {
arr.push(node);
} else {
arr.push.apply(arr, findComments(node));
}
}
return arr;
};
var mentNodes = findComments(document);
//I added this
for (var counter = arr.length; counter > 0; counter--) {
console.log(mentNodes[counter].nodeValue);
}
I keep getting this Error Message:
RangeError: too many arguments provided for a function call debugger eval code:9:13
EDIT: i had a typo while pasting changed the code from i-- to counter--
Share Improve this question edited Oct 23, 2018 at 12:22 BashLaneUp asked Oct 23, 2018 at 12:12 BashLaneUpBashLaneUp 151 silver badge4 bronze badges 6-
In your added
for
loop, you use the variablecounter
in the test expression but then it ends withi--
; should becounter--
. – Pointy Commented Oct 23, 2018 at 12:18 -
Also you moved the declaration of
arr
from outside the function to inside for some reason; probably not a great idea. edit in fact this is almost certainly what's causing your Range Error. – Pointy Commented Oct 23, 2018 at 12:18 - 1 @choz the idea is probably to get a flat array; pushing the array will result in an array of arrays. – Pointy Commented Oct 23, 2018 at 12:19
-
And you moved
arr
initialization outside loop, propably to usearr.length
instead ofmentNodes.length
– barbsan Commented Oct 23, 2018 at 12:19 -
1
Again OP, the problem is that you moved the declaration of
arr
from inside the function to outside. – Pointy Commented Oct 23, 2018 at 12:26
3 Answers
Reset to default 3see this ment in MDN docs about the use of apply
to merge arrays:
Do not use this method if the second array (moreVegs in the example) is very large, because the maximum number of parameters that one function can take is limited in practice. See apply() for more details.
the other note from apply page:
But beware: in using apply this way, you run the risk of exceeding the JavaScript engine's argument length limit. The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function. To illustrate this latter case: if such an engine had a limit of four arguments (actual limits are of course significantly higher), it would be as if the arguments 5, 6, 2, 3 had been passed to apply in the examples above, rather than the full array.
As the array start from index of 0, actually the last item in the array is arr.length - 1
.
you can fix it by:
for (var counter = arr.length - 1; counter >= 0; counter--)
Notice I've added arr.length -1
and counter >= 0
as zero is the first index of the array.
Adding the for
loop is not the only thing you changed (and see the other answer about fixing that loop too). You also moved the declaration of arr
from inside the function to outside, making arr
relatively global.
Because of that, each recursive call to findComments()
works on the same array, and the .apply()
call pushes the entire contents back onto the end of the array every time. After a while, its length exceeds the limit of the runtime.
The original function posted at the top of your question has arr
declared inside the function. Each recursive call therefore has its own local array to work with. In a document with a lot of ment nodes, it could still get that Range Error however.
本文标签: javascriptRangError too many arguments provided for a function callStack Overflow
版权声明:本文标题:javascript - RangError: too many arguments provided for a function call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745051330a2639672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论