I’m working on a greasemonkey script to format by thousands the view count on youtube videos. My function to Format the rough number is doing alright. My problem is setting the formatted number back through an anonymous function. Here is what I’m trying…
function main(arg){
var viewCount = new Array();
for(var i=0; i < arguments.length; i++){
var viewCount = Format(document.getElementsByClassName(arguments[i])[i].textContent);
// (function(){document.getElementsByClassName(arguments[i])[i].innerHTML = viewCount[i]})();
}
}
Now, here is a tricky thing. Debugging it on Firebug, when I remove the comment on the anonymous function, even the var viewCount gets a error saying “ReferenceError: reference to undefined property arguments[i]”. But when a comment it back, it is set as expected.
Here is a complete testbed that I did: http://pastebin.com/JRPRQnv6
arguments[MDN] is a special variable in each function. You are not passing any argument to the anonymous function, hence it is an empty object.If you want to access the
argumentsobject from the outer function, you have to store a reference to it.Example:
But I don’t see a reason to use an immediate function here at all. Another potential problem is that you are accessing the
ith element of.getElementsByClassName, whereiis the loop index for your arguments..getElementsByClassNamemight not even return so many elements (i.e. you are using the wrong index for the wrong list).Maybe you intended to do: