Javascript expando objects

What are expando objects in javascripts?

Why do we need this? Any complete example would be appreciated.

I found 1 Javascript article here : red-haired web development patch siding

+47
javascript expando
Mar 24 '10 at 7:33
source share
4 answers

Well, in javascript, any object is an expando object. This means that, as the article says, whenever you try to access property 1, it will be automatically created.

var myObj = {}; // completely empty object myObj.myProp = 'value'; 

The moment you assign myProp value, the myProp property myProp dynamically created, but it did not exist before. In many other languages, such as C #, this is usually not possible (in fact, C # just turned on support for the expando object, but that is beyond the point). To access a property in a normal class in C #, you need to indicate in the class that it really has this property.

1 Not quite right. See npup Comment below for clarification.

+42
Mar 24 '10 at 7:39
source share

All but primitive types (string, number, boolean) are objects and support. Key: values ​​structure. properties (keys) can be accessed and set using dot notation, as well as square brackets.

 var myObj = {}; myObj.myProp1 = 'value1'; //works, an expando property myObj[myProp2] = 'value2'; // doesn't work, myProp2 is an undefined name. myObj['myProp2'] = 'value2'; // works , an expando property myObj[2010]= 'value'; //note the key is number, still works, an expando property?? myObj.2010 = 'value'; // FAILS. to use dot notation, key must be a string 
+10
Jan 19 2018-11-11T00:
source share

An article written in 2007 that uses document.all (as the only way to access elements)? This is a big red flag.

These are just “You can add properties to an object” outfits with some keywords.

We should be able to do this, because otherwise we would not be able to store data, and this would make JavaScript a rather useless language.

(Is everything an array? No, it's not, and iterates over an object without a hasOwnProperty wrapper. It's not safe. Just stay away from the article, it's worse than useless)

+4
Mar 24 '10 at 7:38
source share

JavaScript turns elements with specific name identifiers into expandos of the returned DOM. This is explained here .

+4
Mar 02 '11 at 16:58
source share



All Articles