4-30 Objects and the DOT

  • (computed member access) is a operator
  • . (dot) is a operator too
  • both has left-to-right asscociativity
  • dot is recommended

types of object properties

computed member access
1
2
3
4
5
6
7
8
9
10

var person = new Object();

person["firstname"] = "Tony";
person["lastname"] = "Alicea";

var firstNameProperty = "firstname";

console.log(person);
console.log(person[firstNameProperty]);
dot operator
1
2
3
4
5
6
7
8
9
10
11
12

var person = new Object();

person["firstname"] = "Tony";
person["lastname"] = "Alicea";

console.log(person.firstname);

person.address = new Object();
person.address.stree = "111 Main St.";
person.address.city = "New York";
person.state = "NY";

4-31 Object and object literal

  • {} is NOT a operator
object literal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

/*
var person {};
= var person = new Object();
*/

var Tony = {
firstname : 'Tony',
lastname : 'Alicea',
address : {
stree : '111 Main St.',
city : 'New York',
state : 'NY'
}
};

function gree(person){
console.log('Hi ' + person.firstname);
}

greet(Tony);

// creating object on the fly
greet({
firstname : 'Mary',
lastname : 'Doe'
});