51

What are expando objects in javascripts?

For what purpose we need this ? Any complete example will be appreciated

I found 1 article here Javascript: The red-headed stepchild of web development

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
xyz
  • 513
  • 1
  • 5
  • 6

4 Answers4

45

Well, in javascript, any object is an expando object. What it means is, as the article covers, that whenever you try to access a property1 it will automatically be created.

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

The moment you assign myProp a value, the property myProp is dynamically created, eventhough it didn't exist before. In a lot of other languages, such as C#, this is not normally possible (actually C# has just enabled expando object support as well, but that's besides the point). To access a property in a normal class in C#, you need to specify in the class that it does indeed have this property.

1 Not quite correct. See npup's comment below for clarification.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • 52
    No.. if you just "try to access" a property it will of course not be "automatically created". You can shove a property in whenever you feel like it, but that's not quite the same thing. Just reaching out for the nonexisting `foo.bar` and getting `undefined` back does not make `foo` *have* the property `bar`. – npup Mar 24 '10 at 09:16
  • 6
    So, to sum up: the expando capability is on *write*, not access. Javascript objects allow you to write a new property to an object without having needed to predefine that property, as is required in some other languages. – XML Dec 12 '13 at 15:15
10

Everything except primitive types(string, number,boolean) are objects and support Key:values structure. properties(keys) can be accessed and set using the dot notation as well as the 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
Community
  • 1
  • 1
Abhijit
  • 1,153
  • 2
  • 16
  • 32
  • 2
    Even primitive types are wrapped automatically into Objects when necessary. `var i = new Number(3); i.someProperty = "someValue"; console.log(i.someProperty);` – igor Mar 28 '11 at 14:58
  • 2
    @igor: "Automatically" is the wrong word, and a little misleading. You explicitly wrapped `i` in a Number object, which allows you to use expando properties. Consequently, this will not work: `var i = 3; i.someProperty = "someValue"; console.log(i.someProperty);` – theazureshadow May 05 '11 at 00:26
  • ah.. sorry.. you are right. Operator 'new' does its job. The following does not work as well: var i = Number(3); i.someProperty = "someValue"; console.log(i.someProperty); – igor May 06 '11 at 09:59
  • @theazureshadow that's not strictly true either though - because `i.toString()` is possible. It will behave as a primitive or an object according to the context with which you use it. – Angry Dan Mar 16 '12 at 12:01
  • 3
    @Sprog: I was unclear. JavaScript *does* wrap primitives using the internal `ToObject` when necessary, but it discards the wrapper as soon as the expression is evaluated. Any property you set is discarded with the wrapper. Check out section 8.7.1 of the [ECMAScript 262 standard](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf). – theazureshadow Mar 17 '12 at 04:09
4

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

It is just dressing up "You can add properties to an object" with some buzzwords.

We need to be able to do this because otherwise we wouldn't be able to store data, and that would make JavaScript a pretty useless language.

(Everything is an array? No it isn't. And it iterates over an object without a hasOwnProperty wrapper. That isn't safe. Just keep away from the article, it is worse than useless)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

JavaScript turns elements with specific IDs of names into expandos of the returned DOM object. It is explained here.

Freeman
  • 5,810
  • 3
  • 47
  • 48