class-继承(es6)

继承-JS

// 动物
function Animal(){
  this.eat = function(){
    console.log('animal eat');
  }
}

// 狗
function Dog(){
  this.bark = function(){
    console.log('dog bark')
  }
}
//显示原型中
Dog.prototype = new Animal();

// 哈士奇
var hashiqi = new Dog

继承-class

class Animal {
  constructor(name){
    this.name = name;
  }
  eat(){
    console.log(`${this.name} eat`);
  }
}

class Dog extends Animal{
  constructor(name){
    super(name);
    this.name = name;
  }
  say(){
    console.log(`${this.name} say`)
  }
}
const dog = new Dog('哈士奇');
dog.say();
dog.eat();
function Animal(){
  this.eat = function(){
    alert('Animal eat')
  }
}

function Dog(){
  this.bark = function(){
    alert('Dog bark')
  }
}
// 绑定原型 实现继承
Dog.prototype = new Animal();
var hashiqi = new Dog();
hashiqi.bark();
hashiqi.eat();

class-总结

Class 在语法上更加贴合面向对象的写法
Class 实现继承更加更易读、易理解
更易于写java等后端语言的使用
本质还是语法糖,使用prototype

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。