关于javascript:JavaScriptES5-ES6-实现继承

ES5

class Human {
  constructor(name) {
      this.name = name
  }
  run() {
      console.log(this.name + ', is runing')
  }
}
class Stu extends Human {
  constructor(name) {
      super(name)
      this.gender = 'male'
  }
  learn() {
      console.log(this.name + ', is learnubg')
  }
}
var stu = new Stu('tao')

ES6

function Human(name) {
  this.name = name
}
Human.prototype.run = function() {
  console.log(this.name + ', is runing')
}
function Stu(name) {
  Human.call(this, name)
  this.gender = 'male'
}
Stu.prototype.__proto__ = Human.prototype
Stu.prototype.learn = function () {
  console.log(this.name + ', is learning')
}
var stu = new Stu('tao')

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理