JS-ES6 Study Notes-class

Source: Internet
Author: User

1, ES6 provides a more approach to the traditional language, introduced the concept of Class (Class) as an object template. classyou 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. constructormethod 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.