1let person = {
2 name : 'John Doe',
3 age : 35
4}
5
6//Now we can add element by 2 ways
7person.occupation = 'Web Designer'
8//or
9person['occupation'] = 'Web Designer'; //This is usefull for adding element within loop.
10
11object[yourKey] = yourValue;
12object.yourKey = yourValue;
13
1//Consider the following example object literal:
2var myObject = {
3 sProp: 'some string value',
4 numProp: 2,
5 bProp: false
6};
7
8//You can use dot syntax to add a new property to it as follows:
9myObject.prop2 = 'data here';
10
11//Modify a Property of an Object Literal
12//The process for modifying a property is essentially the same.
13//Here we will assign a new value to the sProp property shown in the
14//original myObject definition above:
15myObject.sProp = 'A new string value for our original string property';
16
17//Read Current Value of Object Property
18//The following demonstrates how to access a property of an object literal:
19alert(myObject.sProp) // display myObject.sProp value in alert
20var val = myObject.sProp; // assign myObject.sProp to variable