[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
  }
}
原文地址:https://www.cnblogs.com/Answer1215/p/5131518.html