Home Web Front-end Front-end Q&A What is the difference between es6 inheritance and es5 inheritance?

What is the difference between es6 inheritance and es5 inheritance?

Sep 03, 2021 pm 04:08 PM
es5 es6 inherit

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.

What is the difference between es6 inheritance and es5 inheritance?

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
	};
Copy after login

Inherit the parent through subsets:

parent.call(child,1,2)
Copy after login

Let’s look at the underlying method of call. , the inheritance process is through the prototype attribute

child.prototype = new parent(1,2);
Copy after login

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;
}
Copy after login

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‘;
Copy after login

. 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;
    }
}
Copy after login

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());
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? May 01, 2024 pm 10:27 PM

In function inheritance, use "base class pointer" and "derived class pointer" to understand the inheritance mechanism: when the base class pointer points to the derived class object, upward transformation is performed and only the base class members are accessed. When a derived class pointer points to a base class object, a downward cast is performed (unsafe) and must be used with caution.

Solve PHP error: problems encountered when inheriting parent class Solve PHP error: problems encountered when inheriting parent class Aug 17, 2023 pm 01:33 PM

Solving PHP errors: Problems encountered when inheriting parent classes In PHP, inheritance is an important feature of object-oriented programming. Through inheritance, we can reuse existing code and extend and improve it without modifying the original code. Although inheritance is widely used in development, sometimes you may encounter some error problems when inheriting from a parent class. This article will focus on solving common problems encountered when inheriting from a parent class and provide corresponding code examples. Question 1: The parent class is not found. During the process of inheriting the parent class, if the system does not

Detailed explanation of C++ function inheritance: How to debug errors in inheritance? Detailed explanation of C++ function inheritance: How to debug errors in inheritance? May 02, 2024 am 09:54 AM

Inheritance error debugging tips: Ensure correct inheritance relationships. Use the debugger to step through the code and examine variable values. Make sure to use the virtual modifier correctly. Examine the inheritance diamond problem caused by hidden inheritance. Check for unimplemented pure virtual functions in abstract classes.

Detailed explanation of C++ function inheritance: How to understand the 'is-a' and 'has-a' relationship in inheritance? Detailed explanation of C++ function inheritance: How to understand the 'is-a' and 'has-a' relationship in inheritance? May 02, 2024 am 08:18 AM

Detailed explanation of C++ function inheritance: Master the relationship between "is-a" and "has-a" What is function inheritance? Function inheritance is a technique in C++ that associates methods defined in a derived class with methods defined in a base class. It allows derived classes to access and override methods of the base class, thereby extending the functionality of the base class. "is-a" and "has-a" relationships In function inheritance, the "is-a" relationship means that the derived class is a subtype of the base class, that is, the derived class "inherits" the characteristics and behavior of the base class. The "has-a" relationship means that the derived class contains a reference or pointer to the base class object, that is, the derived class "owns" the base class object. SyntaxThe following is the syntax for how to implement function inheritance: classDerivedClass:pu

How do inheritance and polymorphism affect class coupling in C++? How do inheritance and polymorphism affect class coupling in C++? Jun 05, 2024 pm 02:33 PM

Inheritance and polymorphism affect the coupling of classes: Inheritance increases coupling because the derived class depends on the base class. Polymorphism reduces coupling because objects can respond to messages in a consistent manner through virtual functions and base class pointers. Best practices include using inheritance sparingly, defining public interfaces, avoiding adding data members to base classes, and decoupling classes through dependency injection. A practical example showing how to use polymorphism and dependency injection to reduce coupling in a bank account application.

How to use polymorphism and inheritance in PHP to deal with data types How to use polymorphism and inheritance in PHP to deal with data types Jul 15, 2023 pm 07:41 PM

How to use polymorphism and inheritance to handle data types in PHP Introduction: In PHP, polymorphism and inheritance are two important object-oriented programming (OOP) concepts. By using polymorphism and inheritance, we can handle different data types more flexibly. This article will introduce how to use polymorphism and inheritance to deal with data types in PHP, and show their practical application through code examples. 1. The basic concept of inheritance Inheritance is an important concept in object-oriented programming. It allows us to create a class that can inherit the properties and methods of the parent class.

Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Aug 20, 2023 pm 10:49 PM

Inheritance is a concept that allows us to access the properties and behavior of one class from another class. The class that inherits methods and member variables is called a superclass or parent class, and the class that inherits these methods and member variables is called a subclass or subclass. In Java, we use "extends" keyword to inherit a class. In this article, we will discuss a Java program to calculate interest on fixed deposits and time deposits using inheritance. First, create these four Java files - Acnt.java − in your local machine IDE. This file will contain an abstract class ‘Acnt’ which is used to store account details like interest rate and amount. It will also have an abstract method 'calcIntrst' with parameter 'amnt' for calculating

How to force inheritance of proxy final class using Java? How to force inheritance of proxy final class using Java? Sep 06, 2023 pm 01:27 PM

How to force inheritance of proxy final class using Java? In Java, the final keyword is used to modify classes, methods, and variables, indicating that they cannot be inherited, overridden, or modified. However, in some cases, we may need to force inheritance of a final class to achieve specific needs. This article will discuss how to use the proxy pattern to implement such functionality. The proxy pattern is a structural design pattern that allows us to create an intermediate object (proxy object) that can control the behavior of another object (proxy object).

See all articles