Using named keys in a JS array

I am working on the Principles of Object Oriented Javascript and confused by the fact that Zakas uses a named key inside an array (as opposed to inside an object). See Comment:

function EventTarget() {} EventTarget.prototype = { constructor: EventTarget, addListener: function(type, listener) { if (!this.hasOwnProperty("_listeners")) { // Why isn't this: `this._listeners = {};` this._listeners = []; } if (typeof this._listeners[type] === "undefined") { this._listeners[type] = []; } this._listeners[type].push(listener); }, // more stuff } var target = new EventTarget(); target.addListener("message", function(event) { console.log("Message is " + event.data); }); 

Its code works fine (as if you replaced the array for an object literal), but I realized that you have to use the object if you want to access the contents by name. From the w3schools array article :

Many programming languages ​​support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
JavaScript does not support arrays with named indexes.
In JavaScript, arrays always use numbered indexes .

Is there a good reason that Zakas used such an array? Could you explain this? Alternatively, is this what I should submit to the bugs ?

+7
javascript arrays
source share
4 answers

The only reason I see is to confuse people. JavaScript doesn't impose anything, in fact, and since everything is an object, you can do whatever you want. This guy uses an array to store named properties, but he could very well use a function or something else!

Edit: almost everything is an object (I would be interested in someone trying to set the property to undefined , since one of the most common errors in JavaScript is TypeError : undefined is not an object . Sighs JavaScript, why are you doing this with us ?

+5
source share

Is there a good reason that Zakas used such an array?

From the cited code, I can't think of one thing, no. It does not use the fact that _listeners refers to an array. It frankly just looks like a typo. Since it works (because normal JavaScript arrays are objects), a typo was not caught. Well, until you catch it. :-)

If there is no code that you did not quote that then adds the enries * array to this array, there is no reason to use the array there (and possibly for several reasons).

* "array entry" = property whose key is the index of the array. So what is an "array index"? "The property name String P is the index of the array if and only if ToString(ToUint32(P)) is P and ToUint32(P) not 232βˆ’1 ." ( spec )

+2
source share

Arrays are just special objects ....

 a=[] a[7]=9 a["abc"]=20 a["xxx"]=30 for (i in a) console.log("value",i,a[i]) 

Exit;

 value 7 9 value abc 20 value xxx 30 
+2
source share

I am inclined to say that this is a typo, but in an emergency it was conceived, then the only functional use case I can come up with ... ( warning: can share an opinion).

Given that the observer / event pattern is a general concept that can be applied to any object, and the "private" (indicated by the underscore) _listeners probably never be known about the target - then it is that the data contained in it is redundant for the object itself.

That is, it is probably undesirable to transmit this kind of data if the target is serialized. The following example illustrates how the JSON serializer ignores the properties of a non-numeric array in foo.baz - similarly in your own example, all attached event data will be deleted:

 var foo = { bar: {}, baz: [] }; foo.bar['p1'] = foo.baz['p1'] = 1; foo.bar['p2'] = foo.baz['p2'] = 2; console.log( JSON.stringify(foo) ); // {"bar":{"p1":1,"p2":2},"baz":[]} 
0
source share

All Articles