JS-ES6 Study note-class (4)

Source: Internet
Author: User

1 Object.getPrototypeOf . The method can be used to get the parent class from the subclass. Therefore, you can use this method to determine whether a class inherits another class.

2, super this keyword, can be used as a function, can also be used as an object. In both cases, its usage is completely different.

The first case, super as a function call, represents the constructor of the parent class. ES6 requirements, the constructor of a subclass must perform a super function once .

Class A {}class B extends a {  constructor () {    super ();  }}

Note that although the constructor for the parent class is represented, an instance of the subclass is returned, that is super A , the B super internal this refers B to, so it is super() equivalent here A.prototype.constructor.call(this) .

The second case, super as an object, points to the prototype object of the parent class .

class A {  p () {    return 2;  }} Class B extends A {  constructor () {    super ();     // 2 New B ();    

In a subclass B super.p() , super It is used as an object. At this super point, pointing A.prototype , so the super.p() equivalent A.prototype.p() . It is important to note that because of the super prototype object pointing to the parent class, the method or property defined on the parent class instance cannot be super called .

ES6 Specifies that super when a method of the parent class is called, super the this subclass is bound .

3, because of the binding subclass this , if by super assigning a value to a property, then that super is, the this assignment of the property will become the property of the child class instance.

class A {  constructor () {    this. x =1;  }} Class B extends A {  constructor () {    super ();     this. x = 2;     = 3;     // undefined    Console.log (this//  3  new B ();

In the above code, the super.x assignment is 3 equal to the this.x value 3 assigned to. And when read super.x , read is A.prototype.x , so return undefined .

4, when using, super you must explicitly specify whether as a function, or as an object to use, or will error.

5, prototype of the subclass, is the prototype of the parent class. As a result, the behavior of __proto__.__proto__ the parent class instance can be modified through the properties of the subclass instance.

JS-ES6 Study note-class (4)

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.