The JavaScript class introduced in ECMAScript 6 is the syntactic sugar inherited by Javascript's existing Prototype. Class is not a new object-oriented inheritance model added to JavaScript. The classes in JavaScript simply allow us to create objects and handle related inheritance in a more concise and straightforward syntax.
Defining classes
A class is actually a "special function", and just as a function is defined as a function declaration and a function expression, a class is defined in two Ways: a class declaration and a class Expression.
class declaration
A class declaration is a way of defining a class, as follows, class when you use a keyword followed by a class name (here is Polygon), you can define a class.
1 class Polygon {2 constructor (height, Width) {3this . Height = height; 4 this. width = width; 5 }6 }
Variable Promotion
The difference between a class declaration and a function declaration is that the function declaration has a variable elevation phenomenon, and the class declaration does Not. That is, you must declare the class before you can use it, otherwise the code throws an ReferenceError exception, like this:
1 var New // Referenceerror 2 3 class Polygon {}
Class-expression
A class expression is another way to define a class. In a class expression, the class name is Optional. If a class name is defined, the class name can only be accessed within the class Body.
1 //Anonymous.2 varPolygon =class {3 constructor (height, Width) {4 this. Height =height;5 this. width =width;6 }7 };8 9 //named forTen varPolygon =class Polygon { one constructor (height, Width) { a this. Height =height; - this. width =width; - } the};
note: class expressions and class declarations do not have an elevated behavior.
Class body and method definitions
The members of the class need to be defined in a pair of curly braces {} , and the code in the curly braces and the braces itself make up the class Body. Class members include class constructors and class methods, including static methods and instance METHODS.
Strict mode
The code in the class body is forced to execute in strict mode.
Constructors
A constructor method is a special class method that is used to create and initialize an object (generated with that class). A class can have only one constructor method named, otherwise it throws an SyntaxError Exception.
The constructor of the parent class can be called with a keyword in the constructor of the subclass super .
Prototyping methods
See Method Definitions.
1 class Polygon {2 constructor (height, Width) {3 this. Height =height;4 this. width =width;5 }6 7 Get area () {8 return this. CalcArea ()9 }Ten one CalcArea () { a return this. Height * this. width; - } - } theConst SQUARE =NewPolygon (10, 10); - - // - -Console.log (square.area);
Static methods
staticKeyword is used to define a static method of a class. Static methods are those that do not need to be instantiated on a class and can be accessed directly using the class name, and it is important to note that a static method cannot be called by an instantiated Object. Static methods are often used as tool functions.
1 class Point {2 constructor (x, Y) {3 this. x =x;4 this. y =y;5 }6 7 static distance (a, B) {8Const DX = a.x-b.x;9Const DY = a.y-b.y;Ten one returnMath.sqrt (DX*DX + dy*dy); a } - } - theConst P1 =NewPoint (5, 5); -CONST P2 =NewPoint (10, 10); - -Console.log (point.distance (p1, p2));
UseextendsCreating subclasses
extendsKeywords can be used in class declarations or class expressions to create a subclass that inherits a class.
1 class Animal {2 constructor (name) {3 this. Name =name;4 }5 6 speak () {7Console.log ( this. Name + ' makes a noise. ');8 }9 }Ten one class Dog extends Animal { a speak () { -Console.log ( this. Name + ' barks. '); - } the } - - varD =NewDog (' Mitzie '); - //' Mitzie barks. ' +D.speak ();
同样也可以用于原有的原型继承的“类”:
1 functionAnimal (name) {2 this. Name =name; 3 }4Animal.prototype.speak =function () {5Console.log ( this. Name + ' makes a noise. ');6 }7 8 class Dog extends Animal {9 speak () {Ten Super.speak (); oneConsole.log ( this. Name + ' barks. '); a } - } - the varD =NewDog (' Mitzie '); -D.speak ();
It is important to note that a class cannot inherit a generic (non-constructed) object. If you want to create a class that inherits a generic object, you use Object.setPrototypeOf() :
1 varAnimal = {2 speak () {3Console.log ( this. Name + ' makes a noise. ');4 }5 };6 7 class Dog {8 constructor (name) {9 this. Name =name;Ten } one speak () { a Super.speak (); -Console.log ( this. Name + ' barks. '); - } the } - object.setprototypeof (dog.prototype, Animal); - - varD =NewDog (' Mitzie '); +D.speak ();
Species
You might want the array class to MyArray return an Array object. This species mode allows you to rewrite the default Constructor.
For example, when map() you use a method like this to return the default constructor, you want this method to return the object to the parent, instead of Array MyArray 对象。 Symbol.species implementing:
1 class MyArray extends Array {2 //Overwrite species to the parent Array constructor3Static Get [symbol.species] () {returnArray;}4 }5 varA =NewMyArray (a);6 varMapped = A.map (x = = x *x);7 8Console.log (mappedinstanceofMyArray);//false9Console.log (mappedinstanceofArray);//true
UsesuperReferencing a parent class
superKeyword can be used to invoke the constructor or class method of its parent class
class Cat { constructor (name) { this. name= name; } Speak () { Console.log (this. name + ' makes a noise. ') ); }} Class Lion extends Cat { speak () { super.speak (); Console.log (this. name + ' Roars. ') ); }}
Mix-ins
An abstract subclass or mix-ins is a template for a class. A ECMAScript class can have only one parent class, so the behavior of multiple inheritance from a tool class is not possible. Subclasses inherit only the functionality provided by the parent class.
In ECMAScript, a function that takes the parent class as input and uses its subclasses as output can be used to implement Mix-ins:
1 var calculatormixin = base = Class extends Base {2 calc () {}3} ; 4 5 var randomizermixin = base = Class extends Base {6 randomize () {} C15>7 };
Classes that use Mix-ins can be written like this:
1 class Foo {} 2 class Bar extends Calculatormixin (randomizermixin (Foo)) {}
相关:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes
ECMAScript 6 introduced JavaScript class (class)--similar to Java class