1 /**
2 * 继承大体分为俩种:
3 * 构造函数继承
4 * 原型继承
5 * - 拷贝继承
6 * -深拷贝
7 * -浅拷贝
8 * - 原型链继承
9 **/
10
11 function Person(name,age){
12 this.name = name;
13 this.age = age
14 }
15 Person.prototype.sayName = function(){
16 console.log(this.name)
17 }
18
19 //构造函数继承
20 function Person1(name,age,sex){
21 Person.call(this,name,age);
22 this.sex = sex;
23 }
24
25 //原型链继承 : 但我们在调用对象的属性和方法的时候,如果这个对象没有,就去原型链上查找
26 function extend(c){
27 function p(){}; //这个构造函数是空的,这样就不会有多余的一些东西了
28 p.prototype = c.prototype;
29 return new P;
30 }
31 Person1.prototype = extend(Person);
32 Person1.prototype.constructor = Person1;
33
34
35 //浅拷贝:拷贝基本类型,复合类型会拷贝地址,复制第一层
36 for(let attr in Person.prototype){
37 Person1.prototype[attr] = Person.prototype[attr];
38 }
39
40 //深拷贝:每层都拷贝
41 function extend(data){
42 if(typeof data === 'object'){
43 let val = typeof data.length === 'number' ? [] : {};
44 for(var s in data){
45 val[s] = extend(data[s]);
46 }
47 return val;
48 }else{
49 return data;
50 }
51 }