码迷,mamicode.com
首页 > 其他好文 > 详细

es5和es6中怎么声明一个类

时间:2020-01-17 00:14:51      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:声明   不同   func   new   语法   function   console   const   monk   

//es5

let Animal = function(type){

   this.type = type

}

Animal.prototype.eat = function (){

   console.log(‘eat food‘)

}

let dog = new Animal(‘dog‘)

let monkey = new Animal(‘monkey‘)

monkey.constructor.prototype.eat = function (){

   console.log(‘eating‘)

}

dog.eat()

monkey.eat()

//es6

class Animal{

   constructor(type){

      this.type = type

   }

   eat(){

       console.log(‘eating‘)

   }

}

let dog = new Animal(‘dog‘)

let monkey = new Animal(‘monkey‘)

console.log(dog)

console.log(monkey)

dog.eat()

monkey.eat()

语法糖:语法不同,效果一样

es5和es6中怎么声明一个类

标签:声明   不同   func   new   语法   function   console   const   monk   

原文地址:https://www.cnblogs.com/qjb2404/p/12203628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!