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 } }
时间: 2024-10-11 18:28:28