Re: Clarification on "live NodeList"

Boris Zbarsky wrote:
> 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.
Oh! And also this....

It means that call/stack frame here

function foo()
{
   if(..) {
     var one = ...;
  } else {
     var two = ...;
  }
}
needs to be as twice bigger than it is needed. But who cares of course 
these days....

Thanks for the reminder, Boris.

>
>> 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).
I can imagine. That's just highlights responsibility of public standards 
developers.
Forests on this planet will go away slightly sooner because of tons of 
"small" things like that live NodeList.
>
> -Boris
>
>
--
Andrew Fedoniouk.

http://terrainformatica.com

Received on Friday, 10 July 2009 23:24:52 UTC