What is the difference between es6 inheritance and es5 inheritance?
The difference between es5 and es6 inheritance: ES5 inheritance is implemented through the prototype or constructor mechanism; it first creates a subclass, then instantiates the parent class and adds it to the subclass this. ES6 first creates the parent class, then instantiates the subset to access the parent class by calling the super method, and then implements inheritance by modifying this.
The operating environment of this tutorial is: windows7 system, ECMAScript 5&&ECMAScript 6 version, Dell G3 computer.
The difference between es6 inheritance and es5 inheritance
Inheritance of ES5 essentially creates an instance object of the subclass first, and then Add the method of the parent class to this (Parent.apply(this)).
#The inheritance mechanism of ES6 is completely different. In essence, the instance object this of the parent class is created first (so You must first call the super() method of the parent class), and then use the constructor of the subclass to modify this.
Inheritance in ES5 is implemented through the prototype or constructor mechanism.
ES6 defines a class through the class keyword, which has a constructor method, and inheritance between classes is achieved through the extends keyword. Subclasses must call the super method in the constructor method, otherwise an error will be reported when creating a new instance. Because the subclass does not have its own this object, but inherits the this object of the parent class and then processes it. If the super method is not called, the subclass cannot get the this object.
Note that the super keyword refers to an instance of the parent class, that is, the this object of the parent class.
Note: In the subclass constructor, the this keyword can only be used after calling super, otherwise an error will be reported.
1. Inheritance in es5:
function parent(a,b){ this a = a; this b = b; } function child(c){ this c = c };
Inherit the parent through subsets:
parent.call(child,1,2)
Let’s look at the underlying method of call. , the inheritance process is through the prototype attribute
child.prototype = new parent(1,2);
It can be seen that the essence of ES5 inheritance is to first create an instance object of the subclass element child, and then assign the attributes in the prototype object of the parent class element parent To the instance object of the subclass element child, thereby realizing inheritance
2. Inheritance in ES6
In traditional JS, objects are generated by creating constructors. Then define the generated object
function parent(a,b){ this.a = a; this.b = b; }
and then add the corresponding required methods or attributes through prototype
parent.prototype.methods = function(){ return 'this is test methods'; } parent.prototype.attr = 'this is test attr‘;
. The concept of class, that is, class, was introduced in ES6. Objects are defined through the keyword class.
Class is a keyword, language sugar, so that you can more clearly understand the created object.
Receive the parameters passed in by the control method through the attribute constructor. If you do not write this attribute, The default is no parameters
class parent{ curstructor(a,b){ this.a = a; this.b = b; } }
Inheritance in ES6 is based on inheritance between classes. This is achieved through the keyword extends.
Calling the parent class through super instantiation
class parent{ constructor(a,b){ this.a = a; this.b = b; } parentMethods(){ return this.a + this.b } } class child extends parent{ constructor(a,b,c){ super(a,b); this.c = c; } childMethods(){ return this.c + ',' + super.parentMethods() } } const point = new child(1,2,3); alert(point.childMethods());
The above code is a simple set of ES6 parent-child class inheritance.
I believe you have seen it, although the obvious difference is that in ES6, it is the super method that activates the parent component, rather than creating a new instantiation. In other words, the instance object of the parent class is created first. After the call, modify this in the constructor of the subclass to complete the prototype object.
Summary:
The biggest difference between ES5 and ES6 inheritance is:
ES5 creates a subclass first, and then in the instance Transform the parent class and add it to the subclass this
ES6 first creates the parent class, and after accessing the parent class by calling the super method in the instantiated subset, inheritance is realized by modifying this
[Recommended learning: javascript video tutorial]
The above is the detailed content of What is the difference between es6 inheritance and es5 inheritance?. For more information, please follow other related articles on the PHP Chinese website!

HTML and React can be seamlessly integrated through JSX to build an efficient user interface. 1) Embed HTML elements using JSX, 2) Optimize rendering performance using virtual DOM, 3) Manage and render HTML structures through componentization. This integration method is not only intuitive, but also improves application performance.

React efficiently renders data through state and props, and handles user events through the synthesis event system. 1) Use useState to manage state, such as the counter example. 2) Event processing is implemented by adding functions in JSX, such as button clicks. 3) The key attribute is required to render the list, such as the TodoList component. 4) For form processing, useState and e.preventDefault(), such as Form components.

React interacts with the server through HTTP requests to obtain, send, update and delete data. 1) User operation triggers events, 2) Initiate HTTP requests, 3) Process server responses, 4) Update component status and re-render.

React is a JavaScript library for building user interfaces that improves efficiency through component development and virtual DOM. 1. Components and JSX: Use JSX syntax to define components to enhance code intuitiveness and quality. 2. Virtual DOM and Rendering: Optimize rendering performance through virtual DOM and diff algorithms. 3. State management and Hooks: Hooks such as useState and useEffect simplify state management and side effects handling. 4. Example of usage: From basic forms to advanced global state management, use the ContextAPI. 5. Common errors and debugging: Avoid improper state management and component update problems, and use ReactDevTools to debug. 6. Performance optimization and optimality

Reactisafrontendlibrary,focusedonbuildinguserinterfaces.ItmanagesUIstateandupdatesefficientlyusingavirtualDOM,andinteractswithbackendservicesviaAPIsfordatahandling,butdoesnotprocessorstoredataitself.

React can be embedded in HTML to enhance or completely rewrite traditional HTML pages. 1) The basic steps to using React include adding a root div in HTML and rendering the React component via ReactDOM.render(). 2) More advanced applications include using useState to manage state and implement complex UI interactions such as counters and to-do lists. 3) Optimization and best practices include code segmentation, lazy loading and using React.memo and useMemo to improve performance. Through these methods, developers can leverage the power of React to build dynamic and responsive user interfaces.

React is a JavaScript library for building modern front-end applications. 1. It uses componentized and virtual DOM to optimize performance. 2. Components use JSX to define, state and attributes to manage data. 3. Hooks simplify life cycle management. 4. Use ContextAPI to manage global status. 5. Common errors require debugging status updates and life cycles. 6. Optimization techniques include Memoization, code splitting and virtual scrolling.

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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