forked from lfthwjx/effective-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
80 lines (64 loc) · 1.51 KB
/
demo.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
console.log(3 + true); // 4
// 'hello'(2); // "hello" is not a function
// null.x; // Cannot read property 'x' of null
console.log(1 + 1); // 2
console.log('hello' + ' world'); // hello world
console.log(2 + '3'); // 23
console.log('2' + 3); // 23
console.log('2' + '3'); // 23
console.log(1 + '2' + 3); // 123
console.log(2 * '3'); // 6
console.log('8' | '1'); // 9
var x = NaN;
console.log(x === x); // false
var a = {};
console.log(a === a); // true
var b = null;
console.log(b === null); // true
console.log(isNaN(NaN)); // true;
console.log(isNaN({})); // true
console.log(isNaN(undefined)); //true
console.log(isNaN('foo')); // true;
console.log(isNaN({valueOf: 'foo'})); // true
console.log(isNaN({valueOf: function(){return 1}})); // false
console.log('J' + {toString: function(){return 'S'}}); // JS
console.log(1 + {valueOf: function(){return 2}}); // 3
var obj = {
toString: function() {
return 'obj';
},
valueOf: function() {
return 1;
}
};
console.log(1 + obj); // 2
console.log('1' + obj); // 11
// bad
function badPoint(x, y) {
if(!x) {
x = 1;
}
if(!y) {
y = 1;
}
return {
x: x,
y: y
}
}
// good
function point(x, y) {
if(typeof x === undefined || y === undefined) {
return {
x: x || 1,
y: y || 1
}
}
return {
x: x,
y: y
}
}
console.log(badPoint(0, 0)); // { x: 1, y: 1 }
console.log(point(0, 0)); // { x: 0, y: 0 }
console.log(point()); // { x: 1, y: 1 }