Home >Web Front-end >JS Tutorial >Using static methods of Class class in ES6
This time I will bring you the static method of using the Class class in ES6. What are the precautions for using the static method of the Class class in ES6? The following is a practical case, let's take a look.
I have seen es6 stuff before but forgot about it, so I will summarize it again:
A class is equivalent to the prototype of an instance. All methods defined in the class will be inherited by the instance. If you add the static keyword before a method, it means that the method will not be inherited by the instance, but will be called directly through the class. This is called a "static method"
class Foo { static classMethod() { return 'hello'; } } Foo.classMethod() // 'hello' var foo = new Foo(); foo.classMethod() // TypeError: foo.classMethod is not a functionIn the above code, the classMethod method of class Foo is preceded by the
static keyword , indicating that the method is a static method and can be called directly on class Foo (Foo.classMethod()). Instead of calling it on an instance of class Foo. If a static method is called on an instance, an error is thrown indicating that the method does not exist.
class Foo { static classMethod() { return 'hello'; } } class Bar extends Foo { } Bar.classMethod(); // 'hello'In the above code, the parent class Foo has a static method, and the subclass Bar can call this method. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Jquery submits array data and uses springmvc to receive it
How to operate the sequence of code execution in JS order
The above is the detailed content of Using static methods of Class class in ES6. For more information, please follow other related articles on the PHP Chinese website!