A script needed to quickly tell me how many html comments there are on the page and what their content is. Using an anonymous function to recursively traverse the DOM seemed appropriate:
var comments = []; //set up an array where comment contents will be copied to (function(D) { if (8===D.nodeType) comments.push(D.nodeValue); //check if node is a comment D=D.firstChild; while (D) { arguments.callee(D); //recursively look for comments... D=D.nextSibling; //...and remember to iterate over all children of any node } })(document); console.log(comments.join("\r\n")); //list all comments
Fiddle works as expected, but I was curious if it was the same function called over and over again, or there were multiple references to the original function called or there were several identical functions called ... In the end, there was no named link, so how will it work, since the bypass goes deeper? I thought I could verify this by adding the following code to while (D) {...}
I'm confused. 1) Do I really call the same function over and over again, or are there several identical functions called or something else in the game? 2) why arguments.caller does not point to our function? it was explicitly called by him - how does recursion work, no?
javascript anonymous-function recursion
ov
source share