Relearn javascript : Basic Object Concepts

1.Define an Object

1
2
3
4
5
6
7
8
//define an empty object
var user = {}

//or with initial properties
var user = {
"userName" : "bigruan",
"passwd" : "123456"
}

It is not necessary to surround properties with quote if property name is a legal javascript name. For example: quotes are necessary for user-name but optinal for userName.

Legal javascript name: contains letters(upper case or lower case), digitals, _ and $ , but should not begin with digital

Object can nest:

1
2
3
4
5
6
7
8
9
var user = {
userName : "bigruan",
passwd : "123456",
car : {
brand : "Audi",
price : 1000
}
}
console.log(user.car.price); //1000

2.Object Property values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//access by [] or .
console.log(user["userName"]); //bigruan
console.log(user["car"]["price"]); //1000
console.log(user.userName); //bigruan
console.log(user.car.price); //1000

//if property not defined, the value will be undefined
console.log(user.child); // undefined
console.log(user["child"]); // undefined

//if trying to retrieve value from undefined, there will be a TypeError.
var childName = user.child.name; //TypeError
//A good practice to avoid this error
var childName = user.child && user.child.name;

Update property value. if property name was defined, value will be replaced by the new value. if there is no such property, a new property will be added to this Object.

1
2
user.userName = "yulong"; //value of userName is updated
user.child = {}; //new property child is added

Retrieve value by for in loop.

1
2
3
for(var key in user) {
console.log(user[key]);
}

3.Object passed by reference and can not be copied

1
2
3
4
5
var user2 = user;
user2.userName = "bigruan2";
console.log(user.userName);
// userName of user is bigruan2,
// because user and user2 are references to the same object