这篇翻译不完整。请帮忙从英语翻译这篇文章。
该新特性属于 ECMAScript 2015(ES6)规范,在使用时请注意浏览器兼容性。
static关键字定义一个类中的静态方法。
语法
static methodName() { ... }
描述
静态方法可以在类还没有实例化时被调用,但不可以在类实例化后调用。静态方法经常作为程序的工具函数使用。
示例
下面的例子说明了这几点:静态方法是怎么在一个类中被定义的;类中的静态成员是可以被继承的;静态方法什么时候可以被调用,什么时候不可以。
class Tripple {
static tripple(n) {
n = n | 1;
return n * 3;
}
}
class BiggerTripple extends Tripple {
static tripple(n) {
return super.tripple(n) * super.tripple(n);
}
}
console.log(Tripple.tripple());
console.log(Tripple.tripple(6));
console.log(BiggerTripple.tripple(3));
var tp = new Tripple();
console.log(tp.tripple()); // 会输出'tp.tripple is not a function'.
规范
| 规范 | 状态 | 说明 |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Class definitions |
Standard | Initial definition. |
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 42.0 | Nightly build | ? | ? | ? |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|
| Basic support | 未实现 | Nightly build | ? | ? | ? | 42.0 |