search
HomeWeb Front-endJS TutorialSummary of 9 inheritance implementation methods in JavaScript_javascript skills

Unlike class-based programming languages ​​such as C and Java, inheritance in JavaScript is prototype-based. At the same time, because JavaScript is a very flexible language, there are many ways to implement inheritance.

The first basic concept is about constructors and prototype chains. The constructor of the parent object is called Parent, the constructor of the child object is called Child, and the corresponding parent and child objects are parent and child respectively.

There is a hidden attribute [[prototype]] (note not prototype) in the object. In Chrome it is __proto__, but in some environments it is inaccessible. It points to the prototype of this object. When accessing the properties or methods of any object, all properties of the object will be searched first. If not found, the properties on the prototype object will be searched step by step along the prototype chain according to [[prototype]] until found. Otherwise return undefined.

1. Prototype chain inheritance:

The prototype chain is the default way to implement inheritance in JavaScript. If you want a child object to inherit the parent object, the simplest way is to point the prototype attribute of the child object's constructor to an instance of the parent object:

Copy code The code is as follows:

function Parent() {}
function Child() {}
Child.prototype = new Parent()

At this time, Child's prototype attribute has been rewritten and points to a new object, but the constructor attribute of this new object does not correctly point to Child. The JS engine will not automatically complete this work for us, which requires us to manually The constructor property of Child's prototype object repoints to Child:
Copy code The code is as follows:

Child.prototype.constructor = Child

The above is the default inheritance mechanism in JavaScript, which migrates the properties and methods that need to be reused to the prototype object, and sets the non-reusable parts as the object's own properties. However, this inheritance method requires a new instance as the prototype object, which is efficient. It will be lower.

2. Prototypal inheritance (non-prototype chain):

In order to avoid the problem of repeatedly creating prototype object instances in the previous method, you can directly point the prototype of the child object constructor to the prototype of the parent object constructor. In this way, all properties and methods in Parent.prototype can also be reused. At the same time, there is no need to repeatedly create prototype object instances:

Copy code The code is as follows:

Child.prototype = Parent.prototype
Child.prototype.constructor = Child

But we know that in JavaScript, objects exist as reference types. This method actually points the pointers saved in Child.prototype and Parent.prototype to the same object. Therefore, when we want to use the child object prototype If you extend some properties in order to continue inheritance later, the prototype of the parent object will also be rewritten, because there is always only one instance of the prototype object here, which is also the shortcoming of this inheritance method.

3. Temporary constructor inheritance:

In order to solve the above problem, you can use a temporary constructor to act as an intermediate layer. All operations on the child object prototype are completed on the instance of the temporary constructor and will not affect the parent object prototype:

Copy code The code is as follows:

var F = function() {}
F.prototype = Parent.prototype
Child.prototype = new F()
Child.prototype.constructor = Child

At the same time, in order to access the attributes in the parent class prototype in the child object, you can add an attribute pointing to the parent object prototype in the child object constructor, such as uber. In this way, the child object can be accessed directly through child.constructor.uber. to the parent prototype object.

We can encapsulate the above work into a function, and calling this function in the future can easily implement this inheritance method:

Copy code The code is as follows:

function extend(Child, Parent) {
var F = function() {}
F.prototype = Parent.prototype
Child.prototype = new F()
Child.prototype.constructor = Child
Child.uber = Parent.prototype
}

Then you can call it like this:
Copy code The code is as follows:

extend(Dog, Animal)

4. Attribute copy:

This inheritance method basically does not change the relationship of the prototype chain, but directly copies all the attributes in the parent prototype object to the child object prototype. Of course, the copy here only applies to basic data types, and object types only support Pass by reference.

Copy code The code is as follows:

function extend2(Child, Parent) {
var p = Parent.prototype
var c = Child.prototype
for (var i in p) {
         c[i] = p[i]
}
c.uber = p
}

This method reconstructs some prototype attributes, which will be less efficient when building objects, but can reduce the search of the prototype chain. However, I personally feel that the advantages of this method are not obvious.

5. Inheritance between objects:

In addition to the inheritance method between constructors, you can also directly inherit between objects without constructors. That is, copy object attributes directly, including shallow copy and deep copy.

Shallow copy:
Accept the object to be inherited, create a new empty object at the same time, copy the properties of the object to be inherited to the new object and return the new object:

Copy code The code is as follows:

function extendCopy(p) {
var c = {}
for (var i in p) {
         c[i] = p[i]
}
c.uber = p
Return c
}

After the copy is completed, the attributes that need to be rewritten in the new object can be manually rewritten.

Deep copy:
The problem of shallow copy is also obvious. It cannot copy the attributes of the object type but can only pass the reference. To solve this problem, deep copy must be used. The focus of deep copy lies in the recursive call of copy. When the properties of the object type are detected, the corresponding object or array is created and the basic type values ​​are copied one by one.

Copy code The code is as follows:

function deepCopy(p, c) {
c = c || {}
for (var i in p) {
If (p.hasOwnProperty(i)) {
If (typeof p[i] === 'object') {
                                   c[i] = Array.isArray(p[i]) ? [] : {}
                 deepCopy(p[i], c[i])
              } else {
c[i] = p[i]
            }
}
}
Return c
}

An ES5 Array.isArray() method is used to determine whether the parameter is an array. Environments that do not implement this method need to manually encapsulate a shim.
Copy code The code is as follows:

Array.isArray = function(p) {
Return p instanceof Array
}

However, array variables from different frameworks cannot be judged using the instanceof operator, but this situation is relatively rare.

6. Prototypal inheritance:

With the help of the parent object, create a new object prototyped by the parent object through the constructor:

Copy code The code is as follows:

function object(o) {
var n
Function F() {}
F.prototype = o
n = new F()
n.uber = o
Return n
}

Here, the parent object is directly set as the prototype of the child object. The Object.create() method in ES5 is this implementation.

7. Mixed use of prototypal inheritance and attribute copying:

In the prototypal inheritance method, the child object is constructed based on the passed in parent object. At the same time, in addition to the properties provided by the parent object, additional objects that need to be copied can be passed in:

Copy code The code is as follows:

function objectPlus(o, stuff) {
var n
Function F() {}
F.prototype = o
n = new F()
n.uber = o

for (var i in stuff) {
n[i] = stuff[i]
}
Return n
}


8. Multiple inheritance:

This method does not involve the operation of the prototype chain. Multiple objects whose attributes need to be copied are passed in, and all attributes are copied in sequence:

Copy code The code is as follows:

function multi() {
var n = {}, stuff, i = 0,
        len = arguments.length
for (i = 0; i         stuff = arguments[i]
for (var key in stuff) {
n[i] = stuff[i]
}
}
Return n
}

The objects are copied in order according to the order in which they are passed in. That is to say, if the object passed in later contains the same properties as the previous object, the latter will overwrite the former.

9. Constructor borrowing:

The call() and apply() methods in JavaScript are very easy to use, and their function of changing the method execution context can also play a role in the implementation of inheritance. The so-called constructor borrowing refers to borrowing the constructor of the parent object in the child object constructor to operate this:

Copy code The code is as follows:

function Parent() {}
Parent.prototype.name = 'parent'

function Child() {
Parent.apply(this, arguments)
}
var child = new Child()
console.log(child.name)


The biggest advantage of this method is that in the constructor of the sub-object, the sub-object's own properties are completely reconstructed, and the reference type variable will also generate a new value instead of a reference, so any operation on the sub-object Neither will affect the parent object.

The disadvantage of this method is that the new operator is not used during the construction process of the child object, so the child object will not inherit any attributes on the parent prototype object. In the above code, the name attribute of the child will be undefined.

To solve this problem, you can manually set the child object constructor prototype to an instance of the parent object again:

Copy code The code is as follows:

Child.prototype = new Parent()

But this will bring about another problem, that is, the constructor of the parent object will be called twice, once during the borrowing process of the parent object's constructor, and the other time during the inheritance prototype process.

To solve this problem, we need to remove a call to the parent object's constructor. Constructor borrowing cannot be omitted, so the last call can only be removed. Another way to implement prototype inheritance is to iteratively copy:

Copy code The code is as follows:

extend2(Child, Parent)

Just use the extend2() method implemented previously.
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
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment