面向对象(四)

2020-09-09  本文已影响0人  懂会悟

1、传统的面向对象

// 定义object函数
function object(o) {
  function F() {}
  F.prototype = o
  return new F()
}

// 定义寄生式核心函数
function inhreitPrototype(subType, superType) {
  var prototype = object(superType.prototype)
  prototype.constructor = subType
  subType.prototype = prototype
}
// 定义Animal构造函数
function Animal(age) {
  this.age = age
  this.colors = ["red", "green"]
}

// 给Animal添加方法
Animal.prototype.animalFunction = function () {
  console.log("Hello Animal")
}

// 定义Person构造函数
function Person(name, age) {
  Animal.call(this, age)
  this.name = name
}

// 使用寄生组合式核心函数
inhreitPrototype(Person, Animal)

// 给Person添加方法
Person.prototype.personFunction = function () {
  console.log("Hello Person")
}


上一篇 下一篇

猜你喜欢

热点阅读