JavaScript study notes (10) js object inheritance_basic knowledge
1. Prototype chain
//Rarely used alone
View Code
//Define the SuperClass class, which has an attribute property and a method getSuperValue
function SuperClass() {
this.property = true;
}
SuperClass.prototype.getSuperValue = function() {
return this.property;
}
//Define the SubClass class, which has an attribute subproperty and a method getSubValue added later
function SubClass() {
this.subproperty = false;
}
//SubClass class inherits SuperClass class
SubClass.prototype = new SuperClass();
//SubClass class adds a method getSubValue
SubClass.prototype.getSubValue = function() {
return this.subproperty;
}
//Create an instance of the SubClass class
var instance = new SubClass();
alert(instance.getSuperValue());
2. Determine the relationship between the prototype and the instance
The first way is to use the instanceof operator to test the instance and prototype chain. Constructor
alert(instance instanceof Object); //true , is instance an instance of Object?
alert(instance instanceof SuperClass); //true, is instance an instance of SuperClass?
alert(instance instanceof SubClass); //true, is instance an instance of SubClass?
The second way is to use the isPrototypeOf() method to test the prototype that appears in the prototype chain
alert(Object.prototype.isPrototypeOf(instance)); //true
alert(SuperClass.prototype.isPrototypeOf(instance)); //true
alert(SubClass.prototype.isPrototypeOf(instance)); //true
3. Points to note when defining methods using prototype chain inheritance
The order in which methods are defined:
View Code
function SuperClass() {
this.property = true;
}
SuperClass.prototype.getSuperValue = function() {
return this.property;
}
function SubClass() {
this.subproperty = false ;
}
//SubClass inherits SuperClass
SubClass.prototype = new SuperClass(); //This should be written first, and the newly added methods and methods of overriding the superclass should be written later , otherwise the overridden superclass method will never be able to call
//Add new method
SubClass.prototype.getSubValue = function() {
return this.subproperty;
}
//Override the super class method
SubClass.prototype.getSuperValue = function() {
return false;
}
var instance = new SubClass();
alert(instance. getSuperValue()); //fales, the instance of SubClass here calls the getSuperValue() method of SubClass, but blocks the getSuperValue() method of SuperClass.
//Using the SuperClass method will call the getSuperValue() method of SuperClass
Disadvantages of prototype chain inheritance: 1) Sharing properties in the super class, 2) You cannot pass parameters to the constructor of the super class when creating a subclass. All prototype chains are rarely used alone
4. Borrowing constructors
//Rarely used alone
Advantages: Parameters can be passed to the super class. Disadvantages: functions cannot be reused, all classes must use the constructor pattern
View Code
function SuperClass(name) {
this.name = name;
}
function SubClass(){
SuperClass.call(this,"RuiLiang"); / /Inherited SuperClass and passed parameters to SuperClass
this.age = 29; //Instance attributes
}
var instance = new SubClass();
alert(instance.name ); //RuiLiang
alert(instance.age); //29
6. Combination inheritance
//The most commonly used inheritance pattern
View Code
//Create SuperClass
function SuperClass(name) {
this.name = name;
this.colors = ["red","blue"," green"];
}
SuperClass.prototype.sayName = function() {
alert(this.name);
}
////Create SubClass
function SubClass(name,age) {
SuperClass.call(this,name); //Inherited attributes
this.age = age; //Own attributes
}
SubClass. prototype = new SuperClass(); //Inherited method
SubClass.prototype.sayAge = function() { //SubClass adds new method
alert(this.age);
};
//Use
var instance1 = new SubClass("RuiLiang",30);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue ,green,black"
instance1.sayName(); //"RuiLiang"
instance1.sayAge(); //30
var instance2 = new SubClass("XuZuNan",26);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"RuiLiang"
instance2.sayAge(); //30
7. Other inheritance patterns
Prototypal inheritance, parasitic inheritance, parasitic combined inheritance

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.

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

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

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 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.

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).

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,
