I’ve been working on a webkit specific library and in my testing I accidentally put two parameters in the wrong order, and somehow that caused a stack overflow rather than an error. I wanted to see if perhaps the community could provide me with some insight to why this might be causing a stack overflow in Chrome before I filed a bug.
var fn = function (eventType, element, callback) {
var filter = function(eventType, element, callback) {
var length = element.length;
for(var i=0;i<length;i++) {
fn(eventType, element[i], callback);
}
};
if ( element && element.nodeName || element === window ) {
element.addEventListener(eventType, callback, false);
} else if (element && element.length) {
filter(eventType, element, callback);
}
};
I’ve tested this against Chrome stable and Canary and it throws the same error on both. The issue occurs when you call it like this: fn([],"string",function() {}) as opposed to fn("string",[],function() {})
What I’ve discovered is the second parameter needs to be a non empty string, but other than that the first and third parameter are irrelevant when the second is a non-empty string.
I’ve also never filed a bug before, which is why I thought I would ask the community if they could assist me in finding why this is a bug, or why it isn’t a bug, before I did anything.
The function is a modified version of the one at the start of this nettuts+ article. http://net.tutsplus.com/tutorials/javascript-ajax/from-jquery-to-javascript-a-reference/
The code doesn’t actually cause a stack overflow. If you replace the
js5.addEvent(...)line withconsole.log(element[i]);the output in the console is each of characters in the element string variable. See http://jsfiddle.net/WaDWY/Although from following the link I believe that line of code might be
fn(...)instead. What this does is cause an infinite recursion calling filter, fn, filter, fn over and over until the maximum stack size is reached. See: http://jsfiddle.net/WaDWY/1/This is would be expected and not a bug.