Re: Clarification on "live NodeList"

Andrew Fedoniouk wrote:
>>         if(nodeType != null && nodeType != 'undefined') {
>>             var remNodes = atNode.getElementsByTagName(nodeType);
>>         }
>>         else {
>>             var remNodes = atNode.childNodes;
>>         }
>>         var nodeLim = remNodes.length;
>>         for(var x = 0; x<nodeLim; x++) {
>>             atNode.removeChild(remNodes[0]);
>>         }
>>
> I am not sure this piece will run at all.
> remNodes will not be seen outside blocks where they declared: { var 
> remNodes = whatever }

I suggest reading up on variable scoping in ECMAScript.  var has 
function scope.  In fact, the following is a perfectly valid ECMAScript 
construct:

   function foo() {
     alert(x);
     var x;
   }

and will alert undefined.  Removing the var declaration will throw an 
exception due to x not being defined.

> But this counts as a proof that it might be some code in the wild that 
> rely on this.

Sure is (esp. note that this is a library used by all sorts of sites).

-Boris

Received on Friday, 10 July 2009 23:01:49 UTC