其他分享
首页 > 其他分享> > js的六种继承方式

js的六种继承方式

作者:互联网

原型链继承

重点:让新实例的原型等于父类的实例。
特点:1、实例可继承的属性有:实例的构造函数的属性,父类构造函数属性,父类原型的属性。
缺点:1、新实例无法向父类构造函数传参。
   2、继承单一。
   3、所有新实例都会共享父类实例的属性。

function Parent(name) {
  this.name = name || '阿巴'
  this.say = function () {
    console.log(this.name);
  }
}
Parent.prototype.job = function () {
  console.log('my job is teacher');
}
Parent.prototype.age = 24

function Child() {

}

Child.prototype = new Parent()
let child = new Child()
console.log(child.age);
console.log(child.name);

  

构造函数继承

重点:用.call()和.apply()将父类构造函数引入子类函数
特点:1、只继承了父类构造函数的属性,没有继承父类原型的属性。
   2、解决了原型链继承缺点1、2、3。
   3、可以继承多个构造函数属性(call多个)。
   4、在子实例中可向父实例传参。
缺点:1、只能继承父类构造函数的属性。
   2、无法实现构造函数的复用。每次用每次都要重新调用。
   3、每个新实例都有父类构造函数的副本,臃肿。

function Parent(name) {
  this.name = name || '阿巴'
  this.say = function () {
    console.log(this.name);
  }
}
Parent.prototype.job = function () {
  console.log('my job is teacher');
}
Parent.prototype.age = 24

function Child(name) {
  Parent.call(this,name)
}

let child = new Child('zzzz')
let child1 = new Child()
// child.name='阿布'
console.log(child.name);
child1.say()

  

组合继承

重点:结合了两种模式的优点,传参和复用
特点:1、可以继承父类原型上的属性,可以传参,可复用。
   2、每个新实例引入的构造函数属性是私有的。缺点:调用了两次父类构造函数(耗内存),子类的构造函数会代替原型上的那个父类构造函数。

function Parent(name){
  this.name=name
  this.age=12
}

function Child(name){
  Parent.call(this,name)
}

Child.prototype=new Parent()
let child=new Child('wl')
console.log(child.name);
console.log(child.age);

  

原型式继承

重点:用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。
特点:类似于复制一个对象,用函数来包装。
缺点:1、所有实例都会继承原型上的属性。
   2、无法实现复用。(新实例属性都是后面添加的)

function createObj(o){
  function F(){}
  F.prototype=o
  return new F()
}

let person={
  name:'llm',
  age:23,
  say:function(){
    console.log('AAAAA');
  }
}

let person1=createObj(person)
let person2=createObj(person)

console.log(person1.say());
person2.age=24
console.log(person2.age);
console.log(person1.age);

  

寄生式继承

重点:用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。
特点:类似于复制一个对象,用函数来包装。
缺点:1、所有实例都会继承原型上的属性。
   2、无法实现复用。(新实例属性都是后面添加的)

function createObj(o){
  function F(){}
  F.prototype=o
  return new F()
}

let person={
  name:'llm',
  age:23,
  say:function(){
    console.log('AAAAA');
  }
}

let person1=createObj(person)
let person2=createObj(person)

console.log(person1.say());
person2.age=24
console.log(person2.age);
console.log(person1.age);

  

寄生组合式继承

重点:修复了组合继承的问题

function Parent(name){
  this.name=name;
  this.colors=['red','blue','green']
  this.age=0
}

Parent.prototype.getName=function(){
  console.log(this.name);
}

function Child(name,age){
  Parent.call(this,name);
  this.age=age;
  this.name=name;
}

// 关键的三步
// let F=function(){}
// F.prototype=Parent.prototype
// Child.prototype=new F()

// 封装一下
function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}

function prototype(child, parent) {
  var prototype = object(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}


// 当我们使用的时候:
prototype(Child, Parent);

let child1=new Child('lucky',18)
let child2=new Child('lucky')

console.log(child1,);
console.log(child2);

  

标签:function,console,name,六种,age,js,继承,prototype,log
来源: https://www.cnblogs.com/pg-13/p/16262175.html