js实现继承属性和方法

文章详细介绍了JavaScript中实现继承的各种方法,包括ES6的`extends`关键字,原型链继承,组合继承,寄生组合继承,实例继承和拷贝继承,以及方法在函数内部、外部和原型上的区别,以及`class`与构造函数创建实例的差异。
摘要由CSDN通过智能技术生成

首先定义一个父类

function Animal (name, age) {
  this.name = name
  this.age = age
  this.eat = function () {
    console.log('吃', this.name, this.age)
  }
}
Animal.prototype.drink = function () {
  console.log('喝', this.name, this.age)
}

// 实验一下
const test = new Animal('狗', 3)
test.eat()
test.drink()
console.log(test)

运行结果

在这里插入图片描述

1 使用extends实现继承

最简便的继承方式,仅支持ES6+

class Dog extends Animal {
  constructor(name, age, breed) {
    super(name, age)
    this.breed = breed
  }
  cry () {
    console.log('叫', this.name, this.age, this.breed)
  }
}

// 实验一下
  const dog = new Dog('狗', 3, '田园犬');
  dog.eat()
  dog.drink()
  dog.cry()
  console.log(dog)

实验结果

在这里插入图片描述

2 原型链继承

function Dog (name, age, breed) {
  Animal.call(this, name, age);
  this.breed = breed;
  this.cry = function () {
    console.log('叫', this.name, this.age, this.breed)
  }
}
Dog.prototype = new Animal()
Dog.prototype.constructor = Dog

// 实验一下
const dog = new Dog('狗', 3, '田园犬');
console.log(dog)
dog.eat()
dog.drink()
dog.cry()

实验结果

在这里插入图片描述

3 组合继承

function Dog (name, age, breed) {
  Animal.call(this, name, age);
  this.breed = breed;
  this.cry = function () {
    console.log('叫', this.name, this.age, this.breed)
  }
}
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog

// 实验一下
const dog = new Dog('狗', 3, '田园犬');
console.log(dog)
dog.eat()
dog.drink()
dog.cry()

实验结果

在这里插入图片描述

4 寄生组合继承

function Dog (name, age, breed) {
  Animal.call(this, name, age);
  this.breed = breed;
  this.cry = function () {
    console.log('叫', this.name, this.age, this.breed)
  }
}
(() => {
  let Super = function () { };
  Super.prototype = Animal.prototype;
  Dog.prototype = new Super();
  Dog.prototype.constructor = Dog;
})();

// 实验一下
const dog = new Dog('狗', 3, '田园犬');
console.log(dog)
dog.eat()
dog.drink()
dog.cry()

实验结果

在这里插入图片描述

5 实例继承

function Dog (name, age, breed) {
  let dog = new Animal(name, age);
  dog.breed = breed;
  dog.cry = function () {
    console.log('叫', this.name, this.age, this.breed)
  }
  return dog
}

// 实验一下
const dog = new Dog('狗', 3, '田园犬');
console.log(dog)
dog.eat()
dog.drink()
dog.cry()

实验结果

在这里插入图片描述

6 拷贝继承

function Dog (name, age, breed) {
  let animal = new Animal(name, age);
  for (let key in animal) {
    this[key] = animal[key]
  }
  this.breed = breed;
  this.cry = function () {
    console.log('叫', this.name, this.age, this.breed)
  }
}

// 实验一下
const dog = new Dog('狗', 3, '田园犬');
console.log(dog)
dog.eat()
dog.drink()
dog.cry()

实验结果

在这里插入图片描述

7 扩展

7.1 函数中方法定义在函数内部、函数外、prototype上的区别

  • 挂载在函数内部的方法,实例内部会复制构造函数的方法
  • 挂载在原型上的方法,不会去复制。
  • 挂载在内部和原型上的方法都是可以通过实例去调用的
  • 一般来说,如果需要访问构造函数内部的私有变量,我们可以定义在函数内部,其他情况我们可以定义在函数的原型上
  • 在函数外定义的方法,只能用类调用
函数位置调用方式结果
构造函数内函数名直接调用报错
函数外函数名直接调用正常执行
函数外 prototype上函数名直接调用报错
构造函数内实例化对象调用正常执行
函数外实例化对象调用报错
函数外 prototype上实例化对象调用正常执行

7.2 class创建实例与构造函数创建实例

function exa1 (name, age) {
    this.name = name
    this.age = age
    this.innerFunc = function () {
      console.log('inner')
    }
  }
  exa1.prototype.outerFunc = function () {
    console.log('prototype')
  }
  exa1.getClassName = function () {
    console.log('example')
  }
  class exa2 {
    constructor(name, age) {
      this.name = name
      this.age = age
    }
    innerFunc = function () {
      console.log('inner')
    }
    outerFunc () {
      console.log('prototype')
    }
    static getClassName () {
      console.log('example')
    }
  }
  console.log(new exa1(), new exa2())

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值