js类原始和es6方式

//原始创建类
function UIGoods(gd) {
  this.data = gd
  this.choose = 0
}

UIGoods.prototype.getTotalPrice = function () {
  return this.data.price * this.choose
}
const ui1 = new UIGoods({ price: 22 })
console.log(ui1.getTotalPrice())
//es6 创建类
class UIGood {
  constructor(gd) {
    this.data = gd
    this.choose = 0
  }
  getTotalPrice() {
    return this.data.price * this.choose
  }
}
const ui2 = new UIGood({ price: 22 })
console.log(ui2.getTotalPrice())

  

posted @ 2024-03-12 10:07  howhy  阅读(4)  评论(0编辑  收藏  举报