类就是实例的原型,以前我们一般会new一个test(),有在类中(test)定义的方法,都会被实例继承。如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态方法”
class Father {
static testMethod() {
return 'hello';
}
}
Father.testMethod() // 'hello' var Child = new Father(); Child.testMethod() // TypeError: Child.testMethod is not a function
父类的静态方法 ,可以被子类继承
class Father { static testMethod() { return 'hello'; } }
class Child extends Father { } Child.classMethod(); // 'hello' 上面代码中,父类Father有一个静态方法,子类Child可以调用这个方法,因为这是通过extends继承的,不是通过new()实例化得到的 复制代码