


Summary of usage examples of several JavaScript simulations supporting inheritance
In JavaScript, inheritance is not directly supported from methods. Simulation methods can be summarized into four types: construction inheritance, prototype inheritance, instance inheritance and copy inheritance. After mastering it, there is also the mixed continuation method. What is this method? It is to choose a few of the previous four and mix them~
Example of construction continuation method:
//定义一个Collection类型 function Collection(size) { this.size = function(){return size}; //公有方法,可以被继承 } Collection.prototype.isEmpty = function(){ //静态方法,不能被继承 return this.size() == 0; } //定义一个ArrayList类型,它"继承"Collection类型 function ArrayList() { var m_elements = []; //私有成员,不能被继承 m_elements = Array.apply(m_elements, arguments); //ArrayList类型继承Collection this.base = Collection; this.base.call(this, m_elements.length); this.add = function() { return m_elements.push.apply(m_elements, arguments); } this.toArray = function() { return m_elements; } } ArrayList.prototype.toString = function() { return this.toArray().toString(); } //定义一个SortedList类型,它继承ArrayList类型 function SortedList() { //SortedList类型继承ArrayList this.base = ArrayList; this.base.apply(this, arguments); this.sort = function() { var arr = this.toArray(); arr.sort.apply(arr, arguments); } } //构造一个ArrayList var a = new ArrayList(1,2,3); dwn(a); dwn(a.size()); //a从Collection继承了size()方法 dwn(a.isEmpty); //但是a没有继承到isEmpty()方法 //构造一个SortedList var b = new SortedList(3,1,2); b.add(4,0); //b 从ArrayList继承了add()方法 dwn(b.toArray()); //b 从ArrayList继承了toArray()方法 b.sort(); //b 自己实现的sort()方法 dwn(b.toArray()); dwn(b); dwn(b.size()); //b从Collection继承了size()方法
Example of prototypal inheritance method:
//定义一个Point类型 function Point(dimension) { this.dimension = dimension; } //定义一个Point2D类型,"继承"Point类型 function Point2D(x, y) { this.x = x; this.y = y; } Point2D.prototype.distance = function() { return Math.sqrt(this.x * this.x + this.y * this.y); } Point2D.prototype = new Point(2); //Point2D继承了Point //定义一个Point3D类型,也继承Point类型 function Point3D(x, y, z) { this.x = x; this.y = y; this.z = z; } Point3D.prototype = new Point(3); //Point3D也继承了Point //构造一个Point2D对象 var p1 = new Point2D(0,0); //构造一个Point3D对象 var p2 = new Point3D(0,1,2); dwn(p1.dimension); dwn(p2.dimension); dwn(p1 instanceof Point2D); //p1 是一个 Point2D dwn(p1 instanceof Point); //p1 也是一个 Point dwn(p2 instanceof Point); //p2 是一个Point
Examples of instance inheritance:
Before talking about the examples of this method, let’s talk about the limitations of the construction inheritance method, as follows:
function MyDate() { this.base = Date; this.base.apply(this, arguments); } var date = new MyDate(); alert(date.toGMTString); //undefined,date并没有继承到Date类型,所以没有toGMTString方法
Some methods of core objects cannot be inherited by construction because The core object does not perform assignment or initialization operations in the constructor like our custom general objects. How about replacing it with prototypal inheritance? , as follows:
function MyDate(){} MyDate.prototype=new Date(); var date=new MyDate(); alert(date.toGMTString); //'[object]'不是日期对象,仍然没有继承到Date类型!
Now, switch to instance inheritance method:
function MyDate() { var instance = new Date(); //instance是一个新创建的日期对象 instance.printDate = function(){ document.write("<p> "+instance.toLocaleString()+"</p> "); } //对instance扩展printDate()方法 return instance; //将instance作为构造函数的返回值返回 } var myDate = new MyDate(); dwn(myDate.toGMTString()); //这回成功输出了正确的时间字符串,看来myDate已经是一个Date的实例 了,继承成功 myDate.printDate(); //如果没有return instance,将不能以下标访问,因为是私有对象的方法
Copy inheritance method example:
Function.prototype.extends = function(obj) { for(var each in obj) { this.prototype[each] = obj[each]; //对对象的属性进行一对一的复制,但是它又慢又容易引起问题 //所以这种“继承”方式一般不推荐使用 } } var Point2D = function(){ //…… } Point2D.extends(new Point()) { //…… }
Mixed inheritance example:
function Point2D(x, y) { this.x = x; this.y = y; } function ColorPoint2D(x, y, c) { Point2D.call(this, x, y); //这里是构造继承,调用了父类的构造函数 //从前面的例子看过来,这里等价于 //this.base=Point2D; //this.base.call(this,x,y); this.color = c; } ColorPoint2D.prototype = new Point2D(); //这里用了原型继承,让ColorPoint2D以Point2D对象为原型
The above is the detailed content of Summary of usage examples of several JavaScript simulations supporting 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



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

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.

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.

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

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

js methods to refresh the current page: 1. location.reload(); 2. location.href; 3. location.assign(); 4. window.location. Detailed introduction: 1. location.reload(), use the location.reload() method to reload the current page; 2. location.href, you can refresh the current page by setting the location.href attribute, etc.

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
