class is a new feature of es6. In ES6, class (class) was introduced as a template for objects, and classes can be defined through the class keyword; the new class writing method makes the writing of object prototypes clearer, more like the syntax of object-oriented programming, and more understandable. Class is a new basic syntactic sugar structure in ECMAScript. Although ES6 classes appear to support formal object-oriented programming, in fact they still use the concepts of prototypes and constructors behind them, allowing object prototypes to The writing method is clearer,
#The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
class is a new feature of es6. In ES6, class (class) was introduced as a template for objects, and classes can be defined through the class keyword.
ES6 provides a writing method that is closer to traditional languages. The newly introduced class keyword has the ability to formally define classes. Class is a new basic syntactic sugar structure in ECMAScript. Although ECMAScript 6 classes appear to support formal object-oriented programming, in fact, they still use the concepts of prototypes and constructors behind them, allowing object prototypes to The writing method is clearer and more like the syntax of object-oriented programming.
Traditional JS only has the concept of objects and does not have the concept of classes, because JS is an object-oriented language based on prototypes, prototypes The characteristic of objects is that all attributes are shared with new objects.
ES6 introduces the concept of class. Classes can be defined through the class keyword. This is an object-oriented language that is more in line with what we usually understand.
1 2 3 4 5 6 7 8 9 10 11 |
|
Static methods and static properties are properties and methods that use the static keyword
2.1 Static methods
1 2 3 |
|
1 2 |
|
2.2 Static properties
1 |
|
1 2 3 4 5 6 7 8 9 |
|
getter and setter are used to read and transfer values to the attributes of the class.
The value function getter and the storage function setter can customize the assignment and value behavior. When a property only has a getter and no setter, the property is a read-only property and cannot be assigned a value, nor can it be initialized for the first time. .
If the variable is defined as private (defined outside the curly braces of the class), you can only use the getter without the setter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
How to use:
1 2 3 4 |
|
1. When defining a method in a class, do not You can add the function keyword to the method, because the constructor in JS is defined by function, separated by two.
2. Do not separate all methods with commas, otherwise an error will be reported.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of Is the class es5 or es6?. For more information, please follow other related articles on the PHP Chinese website!