Home >Web Front-end >JS Tutorial >What are the functions of static methods of classes in ES6
What I bring to you this time is what the static methods of classes in ES6 do. This article will give you a good analysis.
I have seen es6 stuff before, but I have forgotten it. Let me summarize:
The class is equivalent to the prototype of the 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 function
In the above code, there is the static keyword before the classMethod method of class Foo, indicating that the method is a static method and can be called directly on class Foo (Foo.classMethod()), rather than 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.
Static methods of parent classes can be inherited by subclasses.
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 above introduction. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to use Vue’s custom instructions to complete a drop-down menu
How to click in JS Jump to the logged in personal email
Detailed introduction to the use of require.js
The above is the detailed content of What are the functions of static methods of classes in ES6. For more information, please follow other related articles on the PHP Chinese website!