JavaScript inheritance usage analysis_js object-oriented
Before learning about JavaScript inheritance in depth, first understand the following concepts:
Parent class: the inherited class
Subclass: the inherited class
Super class: that is, the parent class
Abstract Class: A class that is generally not used for instantiation. Its purpose is to inherit from other classes.
Base class: a class that other classes can inherit from
Derived class: a class that is inherited from the base class
Javascript object inheritance usually has the following 5 methods:
1. Object impersonation
2. call() method
3.apply() method
4.Prototype chain
5. Mixed method
A. Object impersonation
The so-called object impersonation is that a new class impersonates an old class (the old class must use the constructor method) to achieve the purpose of inheritance.
eg .1
function people(name,sex,age){ //Use the constructor method
this.name=name;
this.sex=sex;
this.age=age;
this.say=function(){
alert(" My name is " this.name);
};
this.doing=function(){
alert("I am speaking");
};
}
var Marry=new people("Marry","Woman","23");
Marry.say();
Marry.doing();
function white_people(name, sex,age){
this.inherit=people;
this.inherit(name,sex,age);
delete this.inherit;
this.area=function(){
alert("I am in Europe");
}
}
var Tom=new white_people("Tom","man","21");
Tom.say( );
Tom.area();
alert(Tom.age);
In the above example, people is used as the base class of white_people. Remember that the format is
this.inherit=people, used for object impersonation to achieve inheritance; //Pretend
this.inherit(name,sex,age); //Inherit
delete this.inherit; //Delete inheritance
All new attributes and new methods must be deleted after inheritance, in order to avoid overwriting the related attributes and methods of the parent class.
In addition, object impersonation supports multiple inheritance.
eg.2
function worker(pay,work){
this.pay= pay;
this.work=work;
}
function city_worker(name,sex,age,pay,work){
this.inherit=people;
this.inherit(name, sex,age);
delete this.inherit;
this.inherit=worker;
this.inherit(pay,work);
delete this.inherit;
}
var Jerry=new city_worker("Jerry","man","21","$1000","coder");
Jerry.say();
alert(Jerry.work) ;
Object impersonation has a shortcoming: when the multiple inheritance mechanism is implemented, if the base class has the same attributes or methods, it will be inherited from the subsequent class.
B.call The () method
is just a function impersonated by the encapsulated object. In this way, we no longer need to write the "classic" three sentences, but replace it with the following sentence:
Base class.call(object, parameters List)
eg.1
function farmer(name ,sex,age,pay,work){
people.call(this,name,sex,age);
worker.call(this,pay,work);
}
var Nicholas=new farmer("Nicholas","man","27","$3000","irrigator");
Nicholas.say();
alert(Nicholas.pay);
Similarly, call() has a small problem with properties and methods with the same name.
C.apply() method
The same as call(). apply() is also a wrapper function for object impersonation .The format is:
base class.apply(object, parameter array);
eg.1
function white_collar(name,sex,age,pay,work){
people.apply(this,new Array(name,sex,age));
worker.apply(this,[pay,work]);
}
var Jiessie=new white_collar("Jiessie","woman","26","$2500","editor");
Jiessie.say();
alert(Jiessie.work);
Similarly, apply() has a small problem with properties and methods with the same name.
D. Prototype chain
The above three methods all use inheritance in the form of constructors. Correspondingly, there is also inheritance in the form of prototype functions: prototype chain.
eg.1
function blue_collar(){
}
blue_collar.prototype.name="Jean";
blue_collar.prototype.age="33";
blue_collar.prototype.say=function(){
alert("my name is " this.name);
};
function city_blue_collar(){
}
city_blue_collar.prototype=new blue_collar();
var jj=new city_blue_collar;
jj.say();
프로토타입 체인에는 매개변수를 전달할 수 없다는 단점도 있습니다. 또한
E.Mixed 메서드
는 생성자 메서드를 사용하기 때문에 다중 상속을 지원하지 않습니다. 클래스의 속성을 작성하려면 속성 상속을 위해 call() 또는 apply()를 사용
프로토타입 메소드를 사용하여 메소드 작성, 메소드 상속을 위해 프로토타입 체인 사용
eg.1
function beauty(이름,나이){
this.name =name;
this.age=age;
}
beauty.prototype.say=function(){
alert("어린 소녀의 이름" this.name)
function china_beauty(이름,나이,지역){
beauty.call(this,name,age);
this.area=area;
}
china_beauty.prototype=new beauty();
china_beauty.prototype.from= function(){
alert("나는 출신입니다" this.area)
}; var diaochan=new china_beauty(" Diao Chan","16","Lintao");
diaochan.say();
diaochan.from();
alert(diaochan.age);

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.

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

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.

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.

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

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
