Okay I have an interesting question. (Or I think it’s interesting).
the codelet
[code]for(var something in Object) alert(something)
will pop up an alert box for each of “Object”‘s properties and methods telling you it’s name.
You can test this with any inherent javascript object (i don’t exactly recommend you try these as you will get a looot of pop-ups):
[code]for(var something in window) alert(something);
for(var something in document) alert(something);
for(var something in new XMLHttpRequest()) alert(something);
Here’s the anomaly: the Array object.
[code]for(var something in new Array()) alert(something)
returns nothing. Nothing at all. But we all know the Array object has methods and properties, like “.length” and “.push()” and “.pop()” and “.substr()”. Where did they all go?
I understand why they have done this, it’s to provide support for associative arrays:
[code]var associativeArray = new Array();
associativeArray[‘var1’] = ‘hello’;
associativeArray[‘var2’] = ‘world’;
for(var something in associativeArray) alert(something);
So this will just find the two defined variables, “var1” and “var2” rather than finding “length”,”pop”,”push” etc. as well.
But I don’t understand how? How did they do this? How do you hide all the properties and methods in an object, as it were?
If anyone could shed any light on this it would be appreciated.
P.s. I don’t approve of the
[code]associativeArray = new Array()
method. All the associative array is is an empty object that you can add properties to. You should write it
[code]associativeArray = new Object()
and then you wouldn’t need this weird behaviour of the Array object.