assign-六大继承-类继承-深拷贝

# assign-六大继承-类继承-深拷贝

// Object.assign()
// target 目标对象
// sources 源对象
function assign(target, ...sources) {
 if (target === null) {
  throw new TypeError('Cannot convert undefined or null to object');
 }

 let res = new Object(target);
 for (let i = 0; i < sources.length; i++) {
  const source = sources[i];
  if (source) {
   for (let key in source) {
    if (Object.prototype.hasOwnProperty.call(source, key)) {
     res[key] = source[key];
    }
   }
  }
 }
 return res;
}

// 原型链继承  (1.子构造函数的prototype  指向 父组件的实例)
function SuperType1() {
  this.name = "father";
  this.habit = ["打小孩"];
}
function SubType1() {
  this.name = "children";
}
SubType1.prototype = new SuperType1();
let child1 = new SubType1();
let child2 = new SubType1();

// 借用构造函数继承(子实例不会共享所有原型对象了,但不接受传参数)
function SuperType2(name) {
  this.name = name;
  this.habit = ["打小孩"];
}
function SubType2(name) {
  this.name = name;
  SuperType2.call(this, name);
}

let child3 = new SubType2(111); //  自己传的参数无效
console.log(child3.name, "childrenname"); //father

// 组合继承 (原型链+借用构造函数)
// 缺点:调用两次父类函数
function SuperType3(name) {
  this.name = name;
  this.habit = ["打小孩"];
}
function SubType3(name) {
  SuperType3.call(this, name); //这里也调用
}
SubType3.prototype = new SuperType3(); // 这里调用
let child4 = new SubType3("组合继承"); //  自己传的参数无效
console.log(child4.name, "childrenname444"); //father

// 原型式继承(object.create)
// 只针对对象共享
function create(obj) {
  function F() {}
  F.prototype = obj;
  return new F();
}

// 寄生式继承(增强某个对象 然后返回这个对象)
function createAnother(original) {
  let clone = create(original); // 通过调用函数创建一个新对象
  clone.sayHi = function () {
    // 以某种方式增强这个对象
    console.log("你吃屁吗 我增强一个屁");
  };
  return clone; // 返回这个对象
}

// 寄生组合式继承
function inheritPrototype(subType, superType) {
  let createObj = null;
  function F() {}
  F.prototype = superType.prototype;
  console.log(F.prototype);
  createObj = new F();
  console.log(createObj.__proto__, "createObj1");
  console.log(createObj.__proto__ === superType.prototype, "createObj1");

  createObj.constructor = subType; // 增强
  subType.prototype = createObj; // 赋值x
  console.log(createObj, "createObj2");
}
function SuperType5(name) {
  this.name = name;
}
function subType5(name) {
  SuperType5.call(this, name); //这里又调用一次
}
inheritPrototype(subType5, SuperType5);

/**
 * 类继承 用extends 和 super
 */
class Person {
  constructor(name) {
    console.log(name);
    this.name = name;
  }
  show() {
    return `name: ${this.name}`;
  }
}
class User extends Person {
  constructor(name) {
    console.log(name);
    super(name);
  }
  run() {
    return super.show();
  }
}

// 深拷贝
function deepClone(obj, map = new WeakMap()) {
 if (typeof obj !== 'object') return

 if (obj instanceof Data) {
  const copyDate = new Date()
  copyDate.setTime(obj.getTime())
  return copyDate
 }

 if (obj instanceof RegExp) {
  const Constructor = obj.constructor
  return new Constructor(obj)
 }

 let newObj = obj instanceof Array ? [] : {}
 if (map.get(obj)) {
  return map.get(obj)
 }
 map.set(obj, newObj)

 for (let key in obj) {
  if (obj.hasOwnProperty(key)) {
   newObj[key] = obj[key] instanceof Object ? deepClone(obj[key]) : obj[key]
  }
 }
 return newObj
}
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
上次更新: 2022/7/26 下午6:03:32