[TYPO3-dev] WARNING: Is Prototype affecting the inbuilt JavaScript Array and Object types?

Martin Kutschker Martin.Kutschker at n0spam-blackbox.net
Fri Sep 15 15:09:29 CEST 2006


Peter Klein schrieb:
> "Martin Kutschker" <Martin.Kutschker at n0spam-blackbox.net> skrev i en 
> meddelelse news:mailman.1.1158319178.24068.typo3-dev at lists.netfielders.de...
> 
>>Peter Klein schrieb:
>>
>>
>>>Object.prototype.objMerge = function(obj) {
>>
>>This line adds objMerge to ALL objects, not just the ones created with 
>>objMerge.
>>
>>
>>>(If the key isn't numeric, then it's an "Object", not an "Array")
>>
>>No, I can do this:
>>
>>x=new Array();
>>x['a']='b';
>>alert(x['a']);
>>
>>JS Arrays work like PHP arrays. Both are a IMHO horrible mix between 
>>numeric and associative arrays. I guess that PHP borrowed this "feature" 
>>from JS.
>>
> 
> 
> No. Once you use non-numeric keys, it's no longer treated as an Array, but 
> an Object. (But Objects work almost in the same way as Arrays, since an 
> Array is an Object. But an Object is not an Array)
> 
> Definition:
> a.. Array (an ordered sequence of values)
> a.. Object (collection of key/value pairs)
> 
> Most people doesn't know that. (Myself included, until I recently read a lot 
> on JS Arrays/Objects) Thats why several sites/tutorials tells you that 
> Arrays can have non-numeric keys.
> 
> A quick way to test it, is by printing the "length", which is a property of 
> Array, but not Object.
> 
> So for your example it would be:
> 
> alert(x.length)
> 
> If it's an Array, you'll get the length of the Array, but if it's an Object, 
> you'll just get an "undefined" value back..
> 
> 
> Take a look at Quirksmode.org which explains it a bit better than me ;)
> http://www.quirksmode.org/js/associative.html

Reading the mentioned doc I take it that every object is an associative 
array. So a numeric array can take associative values which it can take 
because an Array extends Object.

x=new Array('n','m');
x['a']='b';
alert(x['a']);
alert(x.length);
alert(x[1]);

This code shows 'b' (from the associative array), 2 (length of the numeric 
array) and 'm' (from the numeric array),

So x is still an Array even after I set an "associative value".

Masi




More information about the TYPO3-dev mailing list