[TYPO3-dev] WARNING: Is Prototype affecting the inbuilt JavaScript Array and Object types?
Peter Klein
peter at umloud.dk
Fri Sep 15 15:02:55 CEST 2006
"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
http://www.brainonfire.net/2006/07/06/javascript-object-literal-namespace/
--
Peter Klein/Umloud Untd
More information about the TYPO3-dev
mailing list