其他分享
首页 > 其他分享> > ES5原型继承和ES6原型继承

ES5原型继承和ES6原型继承

作者:互联网

原型

在这里插入图片描述在这里插入图片描述

原型链

在这里插入图片描述

原型继承实现了什么

ES5原型继承方法区别


ES5继承方法描述
原型链继承缺点1:实例化时不能传递参数给父类
缺点2:父类属性为引用类型时,
导致子类引用类型属性改变,
会影响其他子类的属性
借用构造函数可以传递参数,
缺点1:复用性不高
缺点2:只能继承属性不能继承父类方法
组合继承前两个的结合优化,
缺点1:会调用两次SuperType构造函数,
且父类属性保留在原型上
寄生组合继承使用Object.create创建新的引用地址,
解决引用地址共享问题,
且只在借用构造函数传递参数中时调用new
function SuperType() {
  this.obj = [1, 2, 4];
}
SuperType.prototype.method = function () {
  return "父类原型方法";
};
function SubType(name) {
  this.subName = name;
}
SubType.prototype = new SuperType();
// 缺点1:无法子类实例时传递参数给父类
const instanceObj = new SubType("sub");
// 缺点2:当引用类型的属性改变时,会影响共享其他属性值
console.log(instanceObj.obj);
instanceObj.obj.push(3);
const instanceObj2 = new SubType();
console.log(instanceObj2.obj);
function SuperType(name) {
  this.name = name;
  this.arr = [1, 2, 4];
}
SuperType.prototype.method = function () {
  return "父类原型方法";
};
function SubType(name, subName) {
  SuperType.call(this, name);
  this.subName = subName;
}
// 缺点1:难以复用
const instanceObj = new SubType("name", "subName");
console.log(instanceObj);
console.log(instanceObj.method()) //缺点2:报错,子类无法调用父类方法
instanceObj.arr.push(3);
const instanceObj2 = new SubType("name2", "subName2");
console.log(instanceObj2);
function SuperType() {
  this.arr = [1, 2, 4];
}
SuperType.prototype.method = function () {
  return "父类原型方法";
};
function SubType(name, subName) {
  SuperType.call(this, name); // 第二次调用 SuperType
  this.subName = subName;
}
const prototype = new SuperType(); // 第一次调用 SuperType
SubType.prototype = prototype ;

//缺点1:需要调用两次SuperType父类方法
const instanceObj = new SubType("name", "subName");
console.log(instanceObj);
console.log(instanceObj.method());
instanceObj.arr.push(3);
const instanceObj2 = new SubType("name2", "subName2");
console.log(instanceObj2);
function SuperType(name) {
  this.arr = [1, 2, 4];
  this.name = name;
}
SuperType.prototype.method = function () {
  return "父类原型方法";
};
function SubType(name, subName) {
  SuperType.call(this, name);
  this.subName = subName;
}
// 区别是使用了Object.create而不是用new去继承
function inheritPrototype() {
  const prototype = Object.create(SuperType.prototype);
  prototype.constructor = SuperType.constructor;
  // 上两句替代了new SuperType();
  SubType.prototype = prototype;
}
inheritPrototype();
//缺点1:需要调用两次SuperType父类方法
const instanceObj = new SubType("name", "subName");
instanceObj.arr.push(3);
console.log(instanceObj); //实例化对象
console.log(instanceObj.method()); //父类原型方法
const instanceObj2 = new SubType("name2", "subName2");
console.log(instanceObj2);

ES6继承 class关键词

// 父类
class SuperType {
  constructor(property) {
    this.property = property;
  }
  toString() {
    return `父类方法打印:${this.property}`;
  }
}
// 子类
class SubType extends SuperType {
  constructor(property, subProperty) {
    super(property);
    this.subProperty = subProperty;
  }
}
// 实例化对象
const instanceObj = new SubType("父属性", "子属性");
console.log(instanceObj);
console.log(instanceObj.toString());

标签:ES6,name,继承,instanceObj,SubType,原型,SuperType,父类
来源: https://blog.csdn.net/cxylcc/article/details/121222881