I have collected and organized the super keyword in ES6 Class. This article will be shared with everyone. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone.
The following are just personal study notes:
The super keyword can be used as either a function or an object. In both cases, its usage is completely different.
1. Use it as a function
class A {} class B extends A { constructor() { super(); //ES6 要求,子类的构造函数必须执行一次super函数。 } }
Note that although super represents the constructor of parent class A, the returned It is an instance of subclass B, that is, this inside super refers to B, so super() is equivalent to A.prototype.constructor.call(this) here.
class A { constructor() { console.log(new.target.name); //new.target指向当前正在执行的函数 } } class B extends A { constructor() { super(); } } new A() // A new B() // B
You can see that when super() is executed, it points to the constructor of subclass B, not the constructor of parent class A. In other words, this inside super() points to B.
2. Use it as an object
In ordinary methods, it points to the prototype object of the parent class; in static methods, it points to the parent class.
class A { c() { return 2; } } class B extends A { constructor() { super(); console.log(super.c()); // 2 } } let b = new B();
In the above code, super.c() in subclass B uses super as an object. At this time, super points to A.prototype in the ordinary method, so super.c() is equivalent to A.prototype.c().
When calling the method of the parent class through super, super will bind this of the subclass.
class A { constructor() { this.x = 1; } s() { console.log(this.x); } } class B extends A { constructor() { super(); this.x = 2; } m() { super.s(); } } let b = new B(); b.m() // 2
In the above code, although super.s() calls A.prototype.s(), A.prototype.s() will bind the subclass B's this causes the output to be 2, not 1. In other words, what is actually executed is super.s.call(this).
Since this is bound to the subclass, if a property is assigned a value through super, then super is this, and the assigned property will become the property of the subclass instance.
class A { constructor() { this.x = 1; } } class B extends A { constructor() { super(); this.x = 2; super.x = 3; console.log(super.x); // undefined console.log(this.x); // 3 } } let b = new B();
In the above code, super.x is assigned a value of 3, which is equivalent to assigning a value of 3 to this.x. When reading super.x, A.prototype.x is read, so undefined is returned.
Note that when using super, you must explicitly specify whether to use it as a function or an object, otherwise an error will be reported.
class A {} class B extends A { constructor() { super(); console.log(super); // 报错 } }
In the above code, the super in console.log(super) cannot be seen whether it is used as a function or as an object, so when the JavaScript engine parses the code An error will be reported. At this time, if the data type of super can be clearly stated, no error will be reported.
Finally, since objects always inherit other objects, you can use the super keyword in any object.
Related recommendations:
Detailed explanation of the super() function in python Usage and working principle
Super calling details in multiple inheritance
The above is the detailed content of Detailed explanation of super keyword in ES6 Class. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
