0%

源码阅读学到的新知识

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
// 将b的方法复制到a
function merge (a, b){
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
};


// 链式调用
function My() {
this.sum = 0;
}

My.prototype.add = function (a, b) {
this.sum = a + b;
return this;
};

My.prototype.sub = function (b, c) {
this.sum -= b + c;
return this;
};
var my = new My();
console.log(my.add(1, 6).sub(3, 4).sum);


// 不调用new 实例化

function Person(options) {
if(!(this instanceof Person)){
return new Person(options)
}
}