7-65 Initialization

  • use literal notaion to initialization
  • convenient for testing
  • js engine will check syntax for you
simple initialization
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

var people = [
{
firstname : 'John',
lastname : 'Doe',
address : [
'111 Main St.',
'others'
]
},
{
firstname : 'Jane',
lastname : 'Doe',
greet : function (){
return 'Hi ';
}
}
]

7-66 typeof instanceof

  • typeof : return the type name in String

  • some unexpected result : [], undefined, null

  • instanceof : find in deeper prototype chain, check if the type in the chain

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 a = 3;
console.log(typeof a);

var b = "Hello";
console.log(typeof b);

var c = {};
console.log(typeof c);

var d = [];
console.log(typeof d); // weird!
console.log(Object.prototype.toString.call(d)); // better!

function Person(name) {
this.name = name;
}

var e = new Person('Jane');
console.log(typeof e);
console.log(e instanceof Person);

console.log(typeof undefined); // makes sense
console.log(typeof null); // a bug since, like, forever...

var z = function() { };
console.log(typeof z);

7-67 Strict Mode

  • optional
  • must be in the top of file or top of the function
  • not every js engine implement strict mode in the same way

strict mode reference

  • 可能問題
    當多個js files合併時(produciton常用),最開始的js file若使用了strict mode,則後面的files都會受到影響
    不能保證其它lib都遵守strict mode
one circumstance that strict mode helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//'use strict';

function logNewPerson(){
//'use strict';

var person2;
persom2 = {};
console.log(persom2);
}

var person;

persom = {};
console.log(persom);