In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what the es6 keyword super refers to". In the daily operation, I believe that many people have doubts about what the es6 keyword super refers to. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what does the es6 keyword super refer to?" Next, please follow the editor to study!
Es6 keyword super means: 1, as a function, on behalf of the parent constructor, syntax for "constructor () {super ();}"; 2, as an object, on behalf of the parent prototype object, syntax for "console.log (super.name ())".
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
What does the es6 keyword super mean?
ES6 reimplements the inheritance of the class, and in the process of inheritance, the super keyword plays a vital role. It can be said that you can't understand the super keyword, so you can't master the class inheritance. Today, let's disk the keyword super.
First of all, a concept is thrown: the keyword super can be used both as a function and as an object.
The first case: when super is a function, it represents the constructor of the parent class.
ES6 requires that the constructor of a subclass must execute the super function once
Class A {} class B extends A {constructor () {super (); / / the constructor of the subclass must execute the super function once, representing the constructor of the parent class}}
Note: although super represents the constructor of the parent class, the instance of B returned at this time, that is, the this within super refers to the instance of B, so super () is equivalent to A.prototype.constructor.call (this).
In the above code, new.target points to the currently executing function, and when super () executes, it points to the constructor of subclass B, not the constructor of parent class A, that is, the this inside super () points to B.
The second case: when super is an object, in ordinary methods, point to the prototype object of the parent class, and in static methods, point to the parent class
Class A {p () {return 2;}} class B extends A {constructor () {super (); / / constructor of the parent class console.log (super.p ()); / / 2}} let b = new B ()
In the above code, when super is a function, it represents the constructor of the parent class, and when it is an object, it points to the prototype object of the parent class, that is, A.prototype, so super.p () is equivalent to A.prototype.p ().
It is also important to note that because super points to the prototype of the parent class, properties or methods on the instance of the parent class cannot be called through super
In the above code, p is the property of the instance of parent class A, and super.p cannot reference it.
At this point, the study of "what does the es6 keyword super refer to" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.