Home >Web Front-end >Front-end Q&A >What does the static modifier mean in es6?
The static modifier in es6 means to modify member variables and member methods. static means static, that is, defining static methods; static modifying member variables means that only one copy of the member variable is stored in the memory. You can To be accessed and modified by sharing, the class defines static methods through static.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
static means static, it can modify member variables and member methods
static modified member variables means that Member variables only store one copy in memory and can be accessed and modified by sharing.
Class (class) defines static methods through the static keyword. Static methods cannot be called on instances of a class, but should be called through the class itself. These are typically utility methods, such as functions that create or clone objects.
The above statement is relatively simple. If you want to understand it, you need to clarify a few concepts:
In the process of object-oriented programming, the process of creating objects using classes is usually called instantiation. Classes are prototypes of instances. Classes are static and do not occupy process memory, while instances have dynamic memory.
Normally, we will create a new test(), and the methods defined in the class test() will be inherited by the instance. But adding the static keyword before a method 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.
The example is as follows:
First we created the parent class Father, and created the static method testMethod() in this class. When we call the method directly through the Father class, the call can be successful. But if we create an instance Child through the class, the static method will not be inherited on this instance, and of course this method will not be called successfully.
class Father { static testMethod() { return 'hello'; } } Father.testMethod() // output: 'hello' var Child = new Father(); Child.testMethod() // output: TypeError: Child.testMethod is not a function
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of What does the static modifier mean in es6?. For more information, please follow other related articles on the PHP Chinese website!