Skip to content Skip to sidebar Skip to footer

Dash On The End Of Javascript Object Property

can i use dash on the end of javascript object property name like this. I could not find in any documentation that this is not valid but i got some strange results when trying to a

Solution 1:

You'll have to use bracket notation instead of dot notation:

myObject["myProp-"]

Solution 2:

var myObject = {"myProp-":"myValue", "foo": "bar" };

myObject.foo;
myObject["foo"]; // these are equivalent

myObject.myProp-; // syntax error
myObject["myProp-"]; // this is finevar key = "myProp-";
myObject[key]; // this works as well (dynamic index)
myObject.key; // undefined

Bracket notation are dot notation are equivalent.

Post a Comment for "Dash On The End Of Javascript Object Property"