


What are the characteristics of JavaScript inheritance? Examples of js inheritance
This article brings you what are the characteristics of JavaScript inheritance? The explanation of js inheritance examples has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I recently learned the object-oriented approach of JS. This article mainly discusses the things inherited in the object-oriented approach of JS.
Features of inheritance in JS:
1. Subclasses inherit parent classes;
2. Subclasses can use the methods and attributes of parent classes
3. Changes to subclasses do not affect the parent class
The following uses an example to illustrate JS inheritance
This code creates a parent class and its prototype, and also creates a Subclass, and inherits the private properties of the parent class
<script> //这是父类 function Father(name,age,marry){ this.name=name; this.age=age; this.marry=marry; } //父类的原型 Father.prototype.showName=function(){ alert(this.name); } //子类 function Son(name,age,marry,weight){ Father.call(this,name,age,marry); this.weight=weight; } </script>
When the subclass Son wants to inherit the prototype of the parent class, my approach is to do this at the beginning The results of the
<script> //这是父类 function Father(name,age,marry){ this.name=name; this.age=age; this.marry=marry; } //父类的原型 Father.prototype.showName=function(){ alert(this.name); } //子类 function Son(name,age,marry,weight){ Father.call(this,name,age,marry); this.weight=weight; } //错误的做法 Son.prototype=Father.prototype; Son.prototype.showAge=function(){ alert(this.age); } var father=new Father('王大锤',30,true); alert(father.showAge); </script>
can be found that changes to the prototype of the subclass affect the prototype of the parent class, and the prototype of the parent class There is no showAge method in the prototype, which violates the third characteristic of inheritance.
Analysis reason: Line 20 of the above code Son.prototype=Father.prototype; The '=' here is an object on both sides, then it means Reference, if it is a reference If so, changes to the object on the left will definitely affect the object on the right
That’s why changes in the prototype of the subclass affect the prototype of the parent class.
Solution
#Method 1: Core idea, the previous problem is not that '=' is the reference relationship that causes the problem Well, then it is guaranteed that '=' is always an assignment relationship, not a reference. Here we define a Clone() method to copy the parent class object to the child class.
The reason why recursion is used in the Clone() method is that objects may be nested within objects during the copying process.
<script> //这是父类 function Father(name,age,marry){ this.name=name; this.age=age; this.marry=marry; } //父类的原型 Father.prototype.showName=function(){ alert(this.name); } //子类 function Son(name,age,marry,weight){ Father.call(this,name,age,marry); this.weight=weight; } Son.prototype=new Clone(Father.prototype); Son.prototype.showAge=function(){ alert(this.age); } var father=new Father('王大锤',30,true); alert(father.showAge); //通过克隆对象:核心思路是保证 '=' 是赋值的关系,而不是引用,也就是保证 '=' 的右边不是对象 function Clone(obj){ for(var i=0;i<obj.length;i++){ if(typeof(obj[key]=='object')){ this.key=new Clone(obj[key]); }else{ this.key=obj[key]; } } } </script>
At this time, the showAge method of the parent class object is undefined
Method 2: The code is very simple, But it’s hard to imagine that it’s not as easy to understand as the first method. Core idea: Changes in the properties of the object itself will not affect changes in the properties of its constructor.
<script> //这是父类 function Father(name,age,marry){ this.name=name; this.age=age; this.marry=marry; } //父类的原型 Father.prototype.showName=function(){ alert(this.name); } //子类 function Son(name,age,marry,weight){ Father.call(this,name,age,marry); this.weight=weight; } function fn(){} fn.prototype=Father.prototype; Son.prototype=new fn(); Son.prototype.showAge=function(){ alert(this.age); } var father=new Father('王大锤',30,true); alert(father.showAge); //通过克隆对象:核心思路是保证 '=' 是赋值的关系,而不是引用,也就是保证 '=' 的右边不是对象 // Son.prototype=new Clone(Father.prototype); // function Clone(obj){ // for(var i=0;i<obj.length;i++){ // if(typeof(obj[key]=='object')){ // this.key=new Clone(obj[key]); // }else{ // this.key=obj[key]; // } // } // } </script>
Related recommendations:
Detailed explanation of inheritance methods in JS
What are the inheritance methods in js? Introduction to two methods of js inheritance (with code)
The above is the detailed content of What are the characteristics of JavaScript inheritance? Examples of js inheritance. 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.

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

Interface: An implementationless contract interface defines a set of method signatures in Java but does not provide any concrete implementation. It acts as a contract that forces classes that implement the interface to implement its specified methods. The methods in the interface are abstract methods and have no method body. Code example: publicinterfaceAnimal{voideat();voidsleep();} Abstract class: Partially implemented blueprint An abstract class is a parent class that provides a partial implementation that can be inherited by its subclasses. Unlike interfaces, abstract classes can contain concrete implementations and abstract methods. Abstract methods are declared with the abstract keyword and must be overridden by subclasses. Code example: publicabstractcla
