[ES6] Class Inherit

In constructor, you can call parent's constuctor() method by supert();

 

class ShoppingCart {
  constructor(userId){
    this.userId = userId;
    this.products = [];
  }
  
  addProducts(product){
    this.products.push(product);
  }
  
  calculate(){
    super.calculate();
  }
}


class ForumShoppingCart extends ShoppingCart {
  constructor(userId){
    super(userId);
  }

  calculate(){
    let partialCost = // call parent class `calculate` method here
    return partialCost - _calculateDiscount();
  }
  
  _calculateDiscount(){
    //... complex math
  }
}

 

posted @ 2016-01-14 20:02  Zhentiw  阅读(257)  评论(0编辑  收藏  举报