search
HomeWeb Front-endJS TutorialDetailed explanation of inheritance in JavaScript_javascript skills

The concept of js inheritance

The following two inheritance methods are commonly used in js:

Prototype chain inheritance (inheritance between objects)
Class inheritance (inheritance between constructors)
Since js is not a truly object-oriented language like java, js is based on objects and has no concept of classes. Therefore, if you want to implement inheritance, you can use the prototype mechanism of js or the apply and call methods

In object-oriented languages, we use classes to create a custom object. However, everything in js is an object, so how to create a custom object? This requires the use of js prototype:

We can simply think of prototype as a template. The newly created custom objects are all copies of this template (prototype) (actually not copies but links, but this link is invisible. New There is an invisible __Proto__ pointer inside the instantiated object, pointing to the prototype object).

JS can simulate the functions of classes through constructors and prototypes. In addition, the implementation of js class inheritance also relies on the prototype chain.

Prototypal inheritance and class inheritance

Classic inheritance is calling the constructor of the supertype inside the constructor of the subtype.
Strict class inheritance is not very common, and is usually used in combination:

Copy code The code is as follows:

function Super(){
This.colors=["red","blue"];
}

function Sub(){
Super.call(this);
}


Prototypal inheritance is to create new objects with the help of existing objects, and point the prototype of the subclass to the parent class, which is equivalent to joining the prototype chain of the parent class

Prototype chain inheritance

In order for the subclass to inherit the attributes (including methods) of the parent class, you first need to define a constructor. Then, assign the new instance of the parent class to the constructor's prototype. The code is as follows:

Copy code The code is as follows:

<script><br /> Function Parent(){<br /> This.name = 'mike';<br /> } <p> function Child(){<br /> This.age = 12;<br /> }<br /> Child.prototype = new Parent();//Child inherits Parent and forms a chain through the prototype <p> var test = new Child();<br /> alert(test.age);<br /> ​​ alert(test.name);//Get the inherited attributes<br /> //Continue prototype chain inheritance<br /> Function Brother(){ //brother construction<br /> This.weight = 60;<br /> }<br /> Brother.prototype = new Child();//Continue prototype chain inheritance<br /> var brother = new Brother();<br /> alert(brother.name);//Inherits Parent and Child, pops up mike<br /> ​​ alert(brother.age);//pop-up 12<br /> </script>

The above prototype chain inheritance is missing a link, that is Object. All constructors inherit from Object. Inheriting Object is done automatically and does not require us to inherit manually. So what is their affiliation?

Determine the relationship between prototype and instance

The relationship between prototypes and instances can be determined in two ways. Operator instanceof and isPrototypeof() methods:

Copy code The code is as follows:

alert(brother instanceof Object)//true
alert(test instanceof Brother);//false, test is the super class of brother
alert(brother instanceof Child);//true
alert(brother instanceof Parent);//true

As long as it is a prototype that appears in the prototype chain, it can be said to be the prototype of the instance derived from the prototype chain. Therefore, the isPrototypeof() method will also return true

In js, the inherited function is called the super type (parent class, base class is also acceptable), and the inherited function is called the subtype (subclass, derived class). There are two main problems with using prototypal inheritance:
First, literal overriding of the prototype will break the relationship and use the prototype of the reference type, and the subtype cannot pass parameters to the supertype.

Pseudo classes solve the problem of reference sharing and the inability to pass parameters of super types. We can use the "borrowed constructor" technology

Borrow constructor (class inheritance)

Copy code The code is as follows:

<script><br /> Function Parent(age){<br /> This.name = ['mike','jack','smith'];<br /> This.age = age;<br /> } <p> function Child(age){<br />         Parent.call(this,age);<br /> }<br /> var test = new Child(21);<br /> alert(test.age);//21<br /> ​​ alert(test.name);//mike,jack,smith<br /> Test.name.push('bill');<br /> ​​ alert(test.name);//mike,jack,smith,bill<br /> </script>

Although borrowing constructors solves the two problems just mentioned, without a prototype, reuse is impossible, so we need a prototype chain to borrow constructors. This pattern is called combined inheritance

Combined inheritance

Copy code The code is as follows:

<script><br /> Function Parent(age){<br /> This.name = ['mike','jack','smith'];<br /> This.age = age;<br /> }<br /> Parent.prototype.run = function () {<br />           return this.name ' are both' this.age;<br /> };<br /> Function Child(age){<br /> ​​​ Parent.call(this,age);//Object impersonation, passing parameters to the super type<br /> }<br /> Child.prototype = new Parent();//Prototype chain inheritance<br /> var test = new Child(21);//You can also write new Parent(21)<br /> alert(test.run());//mike,jack,smith are both21<br /> </script>

Combined inheritance is a commonly used inheritance method. The idea behind it is to use the prototype chain to inherit prototype properties and methods, and to borrow constructors to inherit instance properties. In this way, function reuse is achieved by defining methods on the prototype, and each instance is guaranteed to have its own attributes.

Usage of call(): Call a method of an object and replace the current object with another object.

Copy code The code is as follows:

call([thisObj[,arg1[, arg2[, [,.argN]]]]])

Prototypal inheritance

This kind of inheritance uses prototypes to create new objects based on existing objects without creating custom types. It is called prototypal inheritance

Copy code The code is as follows:

<script><br /> Function obj(o){<br />           function F(){}<br />             F.prototype = o;<br />            return new F();<br /> }<br /> var box = {<br /> name : 'trigkit4',<br /> arr : ['brother','sister','baba']<br /> };<br /> var b1 = obj(box);<br /> ​​ alert(b1.name);//trigkit4 <p> b1.name = 'mike';<br /> alert(b1.name);//mike <p> alert(b1.arr);//brother,sister,baba<br /> b1.arr.push('parents');<br /> alert(b1.arr);//brother,sister,baba,parents <p> var b2 = obj(box);<br /> ​​ alert(b2.name);//trigkit4<br /> alert(b2.arr);//brother,sister,baba,parents<br /> </script>

Prototypal inheritance first creates a temporary constructor inside the obj() function, then uses the incoming object as the prototype of this constructor, and finally returns a new instance of this temporary type.

Parasitic inheritance

This inheritance method combines the prototype factory pattern with the purpose of encapsulating the creation process.

Copy code The code is as follows:

<script><br /> Function create(o){<br /> var f= obj(o);<br />             f.run = function () {<br />                 return this.arr;//Similarly, the reference will be shared <br />         };<br />          return f;<br /> }<br /> </script>

Small problems with combined inheritance

Combined inheritance is the most commonly used inheritance pattern in js, but the supertype of combined inheritance will be called twice during use; once when creating the subtype, and the other time inside the subtype constructor

Copy code The code is as follows:

<script><br /> Function Parent(name){<br /> This.name = name;<br /> This.arr = ['brother','sister','parents'];<br /> } <p> Parent.prototype.run = function () {<br />          return this.name;<br /> }; <p> function Child(name,age){<br />         Parent.call(this,age);//Second call<br /> This.age = age;<br /> } <p> Child.prototype = new Parent();//First call<br /> </script>

The above code is the previous combination inheritance, so the parasitic combination inheritance solves the problem of two calls.

Parasitic Combinatorial Inheritance

Copy code The code is as follows:

<script><br /> Function obj(o){<br />          function F(){}<br />           F.prototype = o;<br />           return new F();<br /> }<br /> Function create(parent,test){<br />         var f = obj(parent.prototype);//Create object<br />             f.constructor = test;//Enhancement object<br /> } <p> function Parent(name){<br /> This.name = name;<br /> This.arr = ['brother','sister','parents'];<br /> } <p> Parent.prototype.run = function () {<br />          return this.name;<br /> }; <p> function Child(name,age){<br />          Parent.call(this,name);<br /> This.age =age;<br /> } <p> inheritPrototype(Parent,Child);//Inheritance is achieved through here <p> var test = new Child('trigkit4',21);<br /> Test.arr.push('nephew');<br /> alert(test.arr);//<br /> alert(test.run());//Only the method is shared <p> var test2 = new Child('jack',22);<br /> alert(test2.arr);//Quotation problem resolution<br /> </script>

call and apply

The global functions apply and call can be used to change the pointer of this in the function, as follows:

Copy code The code is as follows:

//Define a global function
Function foo() {
console.log(this.fruit);
}

// Define a global variable
var fruit = "apple";
// Customize an object
var pack = {
        fruit: "orange"
};

// Equivalent to window.foo();
foo.apply(window); // "apple", at this time this is equal to window
//This in foo at this time === pack
foo.apply(pack); // "orange"

Statement
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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor