What are the inheritance methods in javascript
The inheritance methods in JavaScript include prototype chain inheritance, borrowed constructor inheritance, combined inheritance, prototype inheritance, parasitic inheritance and parasitic combined inheritance. Among them, combined inheritance is the most commonly used inheritance method.
#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.
If we want to inherit in javascript, we must first provide a parent class. We use Person as the parent class here.
All the constructor names below have no actual meaning, such as Coder, Rocker, etc., they are only used as examples
function Person(name){//给构造函数添加了参数 this.name=name; this.sex="male"; this.say=function(){ console.log(this.name); } } Person.prototype.age=21;//给构造函数添加了原型属性
1. Prototype chain inheritance
function Programmer(){ this.name="Jayee"; } Programmer.prototype=new Person();//子类构造函数.prototype=父类实例 var per1=new Programmer(); console.log(per1);//Programmer {name: "Jayee"} console.log(per1.sex);//male console.log(per1.age);//21 console.log(per1 instanceof Person);//true
Key point: Make the prototype of the new instance equal to the instance of the parent class. Programmer.prototype=new Person();
Features: The attributes that an instance can inherit include: the attributes of the instance's constructor, the attributes of the parent class constructor, and the attributes of the parent class prototype. (The new instance will not inherit the attributes of the parent class instance!)
Disadvantages: 1. The new instance cannot pass parameters to the parent class constructor.
2. Single inheritance.
3. All new instances will share the attributes of the parent class instance. (The attributes on the prototype are shared. If one instance modification
changes the attributes of the prototype (per1.__proto__.sex="female", then per2.sex will also become female), and the other
The prototype properties of an instance will also be modified!)
2. Borrowing constructor inheritance
//借用构造函数继承 function Coder(){ Person.call(this,"Jayee");//重点:借用了Person this.age=18; } var cod1=new Coder(); console.log(cod1); //Coder {name: "Jayee", sex: "male", hobby: "", age: 18, say: ƒ} console.log(cod1.name);//Jayee console.log(cod1.age);//18 console.log(cod1 instanceof Person);//false
Key points: Use .call() and .apply() to introduce the parent class constructor Subclass function (self-execution (copying) of the parent class function in the subclass function)
Features: 1. Only inherits the attributes of the parent class constructor and does not inherit the attributes of the parent class prototype. (It can be seen from
that cod1.age is 18 instead of 21)
2. Solved the shortcomings 1, 2, and 3 of prototype chain inheritance.
3. You can inherit multiple constructor attributes (call multiple).
4. Parameters can be passed to the parent instance in the child instance.
Disadvantages: 1. Only the attributes of the parent class constructor can be inherited.
2. The reuse of constructors cannot be achieved. (You have to call it again every time you use it)
3. Each new instance has a copy of the parent class constructor, which is bloated.
3. Combined inheritance (combined prototype chain inheritance and borrowed constructor inheritance) (commonly used)
//组合继承 function Typer(name){ Person.call(this,name); } Typer.prototype=new Person(); var typ=new Typer("Jayee"); console.log(typ); //Typer {name: "Jayee", sex: "male", hobby: "", say: ƒ} console.log(typ.name);//Jayee,继承了构造函数 console.log(typ.age);//21,继承了父类的原型的属性
Key point: combines the advantages of the two modes, parameter passing and reuse
Features: 1. It can inherit the attributes of the parent class prototype, pass parameters, and be reused.
2. The constructor attributes introduced by each new instance are private.
Disadvantages: The parent class constructor is called twice (memory consumption), and the subclass constructor will replace the
parent class constructor on the prototype
4. Prototype Inheritance
//原型式继承 function Rocker(obj) { //先封装一个函数容器,用来输出对象和承载继承的原型 function F(){} F.prototype=obj;//继承了传入的函数 return new F();//返回函数对象 } var per=new Person();//拿到父类实例 var roc =Rocker(per);//F {} console.log(per.__proto__);//{age: 21, constructor: ƒ} console.log(roc.age);//21,继承了父类函数的属性
Key point: wrap an object with a function, and then return the call of this function. This function becomes an instance or object that can
add attributes at will. object.create() is this principle.
Features: Similar to copying an object and wrapping it with a function.
Disadvantages: 1. All instances will inherit the attributes on the prototype.
2. Unable to achieve reuse. (New instance attributes are added later)
5. Parasitic inheritance
//寄生式继承 function Rocker(obj){ function F(){} F.prototype=obj;//继承了传入的函数 return new F();//返回函数对象 } var per4=new Person(); //以上是原型式继承,给原型式继承再套个壳子传递参数 function Artist(obj){ var roc=Rocker(obj); roc.name="Jayee"; return roc; } var art = Artist(per4) //这个函数经过声明之后就成了可增添属性的对象 console.log(typeof Artist);//function console.log(typeof art);//object console.log(art.name);//Jayee,返回了个roc对象,继承了roc的属性
The key point: It is to put a shell on the prototypal inheritance.
Advantages: No custom type is created, because it is just a shell to return the object (this), and this function naturally becomes the new object created.
Disadvantages: No prototype is used and cannot be reused.
6. Parasitic combined inheritance (commonly used)
Parasite: Return the object within the function and then call
Composition: 1. The prototype of the function is equal to another instance. 2. Use apply or call to introduce another constructor in the function, and you can pass parameters
//寄生式组合式继承 //寄生 function Rocker(obj){ function F(){} F.prototype=obj;//继承了传入的函数 return new F();//返回函数对象 } //Rocker就是F实例的另一种表示法 var roc=new Rocker(Person.prototype); //roc实例(F实例)的原型继承了父类函数的原型 //上述更像是原型链继承,只不过继承了原型属性 //组合 function Sub(){ Person.call(this); //这个继承了父类构造函数的属性 //解决了组合式两次调用构造函数属性的缺点 } //重点 Sub.prototype=roc;//继承了roc实例 roc.constructor=Sub;//一定要修复实例 var sub1=new Sub(); //Sub的实例就继承了构造函数属性,父类实例,roc的函数属性 console.log(sub1.age);//21
Key points: Fixed the problem of combined inheritance
7. Class and extends in ES6
//todo
Related video tutorial sharing: javascript video tutorial
The above is the detailed content of What are the inheritance methods in javascript. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

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

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

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.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more
