Hkc

vuePress-theme-reco Hkc    2025
Hkc Hkc

Choose mode

  • dark
  • auto
  • light
TimeLine
GitHub
author-avatar

Hkc

25

Article

13

Tag

TimeLine
GitHub
  • Vue

  • Websocket

  • JS

    • js 自动复制操作
    • js 完美寄生继承
    • 使用 Promise 封装一个 AJAX
    • 求数组的最大值和最小值
    • 云开发小程序
  • CSS

  • Canvas

  • HTTP

  • GIT

  • SERVER

  • MORE

js 完美寄生继承

vuePress-theme-reco Hkc    2025

js 完美寄生继承

Hkc 2018-03-26 js
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