Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

继承研究 #14

Open
cisen opened this issue Jul 18, 2018 · 0 comments
Open

继承研究 #14

cisen opened this issue Jul 18, 2018 · 0 comments

Comments

@cisen
Copy link
Owner

cisen commented Jul 18, 2018

总结

  1. 原型链的实现是因为new的时候创建的对象会有一个__proto__指针指向实例化函数的prototype对象。继承的实现也是通过Cat.prototype = new Animal(); new的时候创建一个对象和它的__proto__指针,所以继承的原理就是new的时候创建的对象的prototype_or_outer_reference_cp指针会指向上一个prototype对象,而js的继承是通过Cat.prototype = new Animal();实现的
    0.1 new Animal()会执行constructor生成一个包括对Animal prototype方法引用的新对象和__proto__指针,将生成的对象赋值给新的prototype就完成了prototype的继承,添加__proto__指针就完成了溯源
  2. extends会继承父constructor的this的属性,使用super之后还是会继承父的this的属性
  3. 所谓继承,只是继承原型方法prototype(通过cat.prototype=new Animate()),属性this.name(通过执行constructor函数),__proto__要指向继承源的prototype。
  4. 实例化对象是没有prototype对象的,只有函数有。实例化对象可以通过instanceof获取实例化的类
  5. 每一个引用数据都有一个__proto__属性,函数只有一个,类有两个(一个是函数本身,一个是prototype对象)。类的__ptoto__指向Function.prototype,类的prototype对象的__proto__指向继承的类的prototype
  6. new的所有对象都会有一个__proto__指针指向父的prototype
  7. 类的constructor是Function
// 定义一个动物类
function Animal (name) {
  // 属性
  this.name = name || 'Animal';
  // 实例方法
  this.sleep = function(){
    console.log(this.name + '正在睡觉!');
  }
}
// 原型方法
Animal.prototype.eat = function(food) {
  console.log(this.name + '正在吃:' + food);
};
function Cat(){ 
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';

// Test Code
var cat = new Cat();
console.log('1', cat instanceof Animal); //true 
console.log('2', Cat instanceof Animal); // false
console.log('3', cat instanceof Cat); //true
console.log('4', cat.__proto__ === Cat.prototype) // true
console.log('5', cat.__proto__ === Animal.prototype) // false
console.log('6', cat.__proto__.__proto__ === Animal.prototype) // true
console.log('7', Cat.__proto__ === Animal.prototype) // false
console.log('7.1', Cat.prototype.__proto__ === Animal.prototype) // true
console.log('8', Cat.__proto__ === Function.prototype) // true
console.log('9.0', cat.constructor, Cat.constructor, Cat.prototype.constructor) // Animal/Function/Animal,因为Cat的constructor美修复
console.log('9', cat.constructor === Cat) // false
console.log('9.1', cat.constructor === Animal) // true
console.log('10', Cat.prototype.constructor === Cat) // false 因为Cat的constructor美修复
console.log('11', Cat.prototype.constructor === Animal) // true 因为Cat的constructor美修复
console.log('12', Cat.constructor === Cat) // false
  1. 注意ES5标准13.2.2If Type(result) is Object then return result.如果new 构造函数返回的是一个对象,则获取到的是该对象,而不是new的类对象(包括数组)。具体实现再ecma_op_function_construct函数的/* 9. */‘

继承的实现方式

es6的extends

会继承父constructor的this的属性

原型链继承

核心: 将父类的实例作为子类的原型

// 定义一个动物类
function Animal (name) {
  // 属性
  this.name = name || 'Animal';
  // 实例方法
  this.sleep = function(){
    console.log(this.name + '正在睡觉!');
  }
}
// 原型方法
Animal.prototype.eat = function(food) {
  console.log(this.name + '正在吃:' + food);
};
function Cat(){ 
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.eat('fish'));
console.log(cat.sleep());
console.log(cat instanceof Animal); //true 
console.log(cat instanceof Cat); //true

特点:

  1. 非常纯粹的继承关系,实例是子类的实例,也是父类的实例

  2. 父类新增原型方法/原型属性,子类都能访问到

  3. 简单,易于实现
    缺点:

  4. 要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行,不能放到构造器中

  5. 无法实现多继承

  6. 来自原型对象的引用属性是所有实例共享的(详细请看附录代码: 示例1)

  7. 创建子类实例时,无法向父类构造函数传参
    推荐指数:★★(3、4两大致命缺陷)

构造继承

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true

特点:

  1. 解决了1中,子类实例共享父类引用属性的问题

  2. 创建子类实例时,可以向父类传递参数

  3. 可以实现多继承(call多个父类对象)
    缺点:

  4. 实例并不是父类的实例,只是子类的实例

  5. 只能继承父类的实例属性和方法,不能继承原型属性/方法

  6. 无法实现函数复用,每个子类都有父类实例函数的副本,影响性能
    推荐指数:★★(缺点3)

实例继承

核心:为父类实例添加新特性,作为子类实例返回

function Cat(name){
  var instance = new Animal();
  instance.name = name || 'Tom';
  return instance;
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // false
```js
特点:

不限制调用方式,不管是new 子类()还是子类(),返回的对象具有相同的效果
缺点:

实例是父类的实例,不是子类的实例
不支持多继承
推荐指数:★★

### 拷贝继承
```js
function Cat(name){
  var animal = new Animal();
  for(var p in animal){
    Cat.prototype[p] = animal[p];
  }
  Cat.prototype.name = name || 'Tom';
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true

特点:

支持多继承
缺点:

  1. 效率较低,内存占用高(因为要拷贝父类的属性)
  2. 无法获取父类不可枚举的方法(不可枚举方法,不能使用for in 访问到)
    推荐指数:★(缺点1)

组合继承

核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}
Cat.prototype = new Animal();

// 感谢 @学无止境c 的提醒,组合继承也是需要修复构造函数指向的。

Cat.prototype.constructor = Cat;

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
Reflect.getPrototypeOf(Cat.prototype);
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true

特点:

  1. 弥补了方式2的缺陷,可以继承实例属性/方法,也可以继承原型属性/方法

  2. 既是子类的实例,也是父类的实例

  3. 不存在引用属性共享问题

  4. 可传参

  5. 函数可复用
    缺点:

  6. 调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)
    推荐指数:★★★★(仅仅多消耗了一点内存)

寄生组合继承

核心:通过寄生方式,砍掉父类的实例属性(父类的this.属性),这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}
(function(){
  // 创建一个没有实例方法的类
  var Super = function(){};
  Super.prototype = Animal.prototype;
  //将实例作为子类的原型
  Cat.prototype = new Super();
  Cat.prototype.constructor = Cat;
})();
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true

感谢 @Bluedrink 提醒,该实现没有修复constructor。

Cat.prototype.constructor = Cat; // 需要修复下构造函数
特点:

堪称完美
缺点:

实现较为复杂
推荐指数:★★★★(实现复杂,扣掉一颗星)

附录代码:
示例一:

function Animal (name) {
  // 属性
  this.name = name || 'Animal';
  // 实例方法
  this.sleep = function(){
    console.log(this.name + '正在睡觉!');
  }
  //实例引用属性
  this.features = [];
}
function Cat(name){
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
var tom = new Cat('Tom');
var kissy = new Cat('Kissy');

console.log(tom.name); // "Animal"
console.log(kissy.name); // "Animal"
console.log(tom.features); // []
console.log(kissy.features); // []

tom.name = 'Tom-New Name';
tom.features.push('eat');

//针对父类实例值类型成员的更改,不影响
console.log(tom.name); // "Tom-New Name"
console.log(kissy.name); // "Animal"
//针对父类实例引用类型成员的更改,会通过影响其他子类实例
console.log(tom.features); // ['eat']
console.log(kissy.features); // ['eat']

原因分析:

关键点:属性查找过程

执行tom.features.push,首先找tom对象的实例属性(找不到),
那么去原型对象中找,也就是Animal的实例。发现有,那么就直接在这个对象的
features属性中插入值。
在console.log(kissy.features); 的时候。同上,kissy实例上没有,那么去原型上找。
刚好原型上有,就直接返回,但是注意,这个原型对象中features属性值已经变化了。

react源码对该继承方式的封装(个人注释可能有误)

/**
 * Helper to reduce boilerplate when creating subclasses.
 帮助者在创建子类时减少样板。
 总结:其实就是将SyntheticEvent和class的prototype合并赋给class,更新Interface,然后再赋予批量创建的方法augmentClass?
 *
 * @param {function} Class
 * @param {?object} Interface
 */
 // 用于生成子类
 // class一般就是SyntheticInputEvent之类的
 // 用于给Class增加this(有可能指向SyntheticUIEvent)的prototype,并且不会增加this构造函数里面的this的一些属性
 // Class和this的关系不是相同的,比如this是SyntheticUIEvent, Class是SyntheticFocusEvent
SyntheticEvent.augmentClass = function(Class, Interface) {
  // this指向SyntheticUIEvent的proxy
  const Super = this;
  // 存在的意义是使Class合并this时不会将this构造函数的this属性合并下来
  const E = function() {};
  // 继承SyntheticUIEvent类的原型方法
  // Super.prototype就是SyntheticUIEvent.prototype,E.constructor指向空函数,并不会指向SyntheticUIEvent
  // 但是E.prototype.constructor会指向Super.prototype.constructor
  E.prototype = Super.prototype;
  // prototype.constructor会指向SyntheticUIEvent
  const prototype = new E();
 // prototype继承SyntheticUIEvent的prototype方法后,再包含Class的原型属性,并作为Class的原型对象
 // Class.prototype一般是空的
  Object.assign(prototype, Class.prototype);
  // prototype集合了SyntheticUIEvent和class的prototype
  // 这样会使Class的constructor指向SyntheticUIEvent,下面修正
  Class.prototype = prototype;
  // Class构造函数使用时预先将SyntheticEvent作为普通函数使用生成实例
  // 再使用SyntheticEvent.augmentClass修改Class类的原型对象
  // 指定前Class.prototype.constructor指向SyntheticEvent,指定后指向Class
  // 详见https://stackoverflow.com/questions/4012998/what-it-the-significance-of-the-javascript-constructor-property/4013295#4013141,
  // https://www.cnblogs.com/SheilaSun/p/4397918.html
  Class.prototype.constructor = Class;

  Class.Interface = Object.assign({}, Super.Interface, Interface);
  // 使Class具有SyntheticUIEvent构造函数形式的生成子类的功能
  Class.augmentClass = Super.augmentClass;
  addEventPoolingTo(Class);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant