1, ES6 provides a more approach to the traditional language, introduced the concept of Class (Class) as an object template. class
you can define a class by keyword.
2.
// Defining Classes class Point { constructor (x, y) { this. x = x; this. y = y; } ToString () { returnthis this. y + ') '; }}
The above code defines a "class", and you can see that there is a constructor
method in it, that is, the constructor method, and the this
keyword represents the instance object. That is, the constructor of ES5, which corresponds to the constructor of the Point
ES6 Point
class.
3, the definition of "class" method, the front does not need to add function
this keyword, directly put the function definition in the can. In addition, the method does not need a comma separation, plus will error.
4, the properties of the constructor prototype
, the ES6 "class" continues to exist. In fact, all methods of a class are defined above the properties of the class prototype
.
class Point { constructor () { // ... } toString () { // ... } tovalue () { // ... }}// equals = { toString () {}, Tovalue () {}};
5 Object.assign
. Methods can easily add multiple methods to a class at a time.
class Point { constructor () { // ... }}object.assign (point.prototype, { toString () {}, Tovalue () {}});
6. All defined methods within the class are non-enumerable (non-enumerable). This is inconsistent with the behavior of ES5.
7, the Class property name, you can use an expression.
Let MethodName = "Getarea"; class square{ Constructor (length) { // ... } [MethodName] () { // ... }}
In the above code, the Square
method name of a class getArea
is obtained from an expression.
8, the constructor
method is the default method of the class, new
the method is called automatically when the object instance is generated by the command. A class must have constructor
methods, and if not explicitly defined, an empty constructor
method is added by default. constructor
method returns an instance object (that is) by default this
, and you can specify that another object be returned entirely.
9, the class's constructor, does not use new
is unable to call, will error.
10. You can __proto__
add a method to class by using the properties of the instance.
var New Point (2,3); var New Point (3,2functionreturn ' Oops '/ / "Oops"// "Oops" var New Point (4,2// "Oops"
11, use the properties of the instance to __proto__
rewrite the prototype, it must be very cautious, not recommended, because it will change the original definition of class, affecting all instances.
12, class does not exist variable promotion (hoist).
Js-es6 Learning Note-class