JavaScript 进阶:高级特性与模块应用
1. 关键字 super 和静态方法
在 JavaScript 中, super 关键字必须在 this 关键字之前使用,通常在构造函数的第一条语句中使用。它还可以用于访问父类的属性和方法。例如:
toString() {
let chainResult = super.toString();
return `${chainResult}, Tax: ${this.getPriceIncTax()}`;
}
上述代码中, TaxedProduct 类定义的 toString 方法调用了父类的 toString 方法,这相当于重写原型方法。
静态方法使用 static 关键字创建,通过类而不是类创建的对象来访问。示例如下:
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
toString() {
return `toString: Name: ${this.name}, Price: ${this.price}`;
}
}
超级会员免费看
订阅专栏 解锁全文
1549

被折叠的 条评论
为什么被折叠?



