JavaScript - Access properties of Object

1.Use bracket for accessing properties of Object. Do not forget "" for property name within bracket.

1:  var dog = {  
2:   species: "greyhound",  
3:   weight: 60,  
4:   age: 4  
5:  };  
6:  var species = dog["species"];  
7:  var weight = dog["weight"];  
8:  var age = dog["age"];  

2.Use dot notation for accessing properties of Object.

1:  var bob = {  
2:   name: "Bob Smith",  
3:   age: 30  
4:  };  
5:  var susan = {  
6:   name: "Susan Jordan",  
7:   age: 25  
8:  };  
9:  var name1 = bob.name;  
10:  var age1 = bob.age;  
11:  var name2 = susan.name;  
12:  var age2 = susan.age;  

No comments:

Post a Comment