JavaScript object-oriented programming (inheritance implementation)
Many OO languages support two inheritance methods: interface inheritance and implementation inheritance. Interface inheritance only inherits method signatures, while implementation inheritance inherits the actual methods. As mentioned before, interface inheritance is not possible in ECMAScript since functions have no signatures. ECMAScript only supports implementation inheritance, and its implementation inheritance mainly relies on the prototype chain. Here, we mainly explain prototype chain inheritance, borrowed constructor, combined inheritance, prototype inheritance, parasitic inheritance, parasitic combination inheritance, etc.
1. Prototype chain
ECMAScript describes the concept of prototype chain and uses prototype chain as the main method to implement inheritance. The basic idea is to use prototypes to let one reference type inherit the properties and methods of another reference type. Let’s briefly review the relationship between constructors, prototypes, and instances: Each constructor has a prototype object, the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object. So, what happens if we make the prototype object equal to an instance of another type? Obviously, the prototype object at this time will contain a pointer to another prototype, and accordingly, the other prototype also contains a pointer to another constructor. If another prototype is an instance of another type, then the above relationship still holds, and so on, layer by layer, a chain of instances and prototypes is formed. This is the basic concept of the so-called prototype chain.
function Person(){ this.name=”defaultName”; } Person.property.doAction=function(){ alert(“talk”); } function Student(){ this.age=5; } Student.property=new Person(); Student.property.doSome=function(){ alert(“ homework”); }
Although the prototype chain is very powerful and can be used to implement inheritance, it also has some problems. The main problem arises from prototypes containing reference type values. You may still remember that we introduced earlier that prototype properties containing reference type values will be shared by all instances; this is why properties should be defined in the constructor instead of in the prototype object. When inheritance is implemented through prototypes, the prototype actually becomes an instance of another type. Therefore, the original instance attributes have naturally become the current prototype attributes. The second problem with the prototype chain is that when creating an instance of a subtype, you cannot pass parameters to the constructor of the supertype. In fact, it should be said that there is no way to pass parameters to the constructor of a supertype without affecting all object instances. Because of this, and the problems just discussed with reference types in prototypes, prototype chains alone are rarely used in practice.
2. Borrowing constructor
In the process of solving the problems caused by containing reference type values in prototypes, developers began to use a method called borrowing constructor (constructor stealing) technique (sometimes called fake objects or classical inheritance). The basic idea of this technique is quite simple, which is to call the supertype constructor inside the subtype constructor. Don't forget that functions are nothing but objects that execute code in a specific environment, so by using the apply() and call() methods you can also execute constructors on (in the future) newly created objects.
function Person(name){ this.name=name; } Person.property.doAction=function(){ alert(“talk”); } Person.property.showName=function(){ alert(this.name); } function Student(){ Person.call(this,name); this.age=5; }
If you just borrow the constructor, you will not be able to avoid the problems of the constructor pattern - Methods are all defined in the constructor, so function reuse is out of the question. Moreover, methods defined in the prototype of the super type are also invisible to the subtype. As a result, all types can only use the constructor pattern. Considering these problems, the technique of borrowing constructors is rarely used alone.
3. Combination inheritance
Combination inheritance (combination inheritance), sometimes also called pseudo-classical inheritance, refers to the combination of prototype chain and borrowed constructor technology, so as to bring out the best of both. An inheritance model. The idea behind it is to use the prototype chain to achieve inheritance of prototype properties and methods, and to achieve inheritance of instance properties by borrowing constructors. In this way, function reuse is achieved by defining methods on the prototype, and each instance is guaranteed to have its own attributes.
function Person(name){ this.name=name; this.loves=[“sing”,”paly games”] } Person.property.showLoves=function (){ alert(this.lovers); } function Student(name,age){ Person.class(this,name); This.age=age; } Student.property=new Person(); Student.property.constructor=Student; Student.property.showName=function(){ alert(this.name); }
Combined inheritance avoids the shortcomings of prototype chains and borrowed constructors, combines their advantages, and becomes the most commonly used inheritance pattern in JavaScript. Furthermore, instanceof and isPrototypeOf() can also be used to identify objects created based on combined inheritance.
4. Prototypal inheritance
function object(o){ function F(){} F.prototype = o; return new F(); }
When there is no need to go to great lengths to create a constructor, but you just want one object to remain similar to another object, prototype Formal inheritance is perfectly capable. But don't forget that properties containing reference type values will always share the corresponding value, just like using the prototype pattern.
5. Parasitic combined inheritance
The so-called parasitic combined inheritance means inheriting properties by borrowing constructors and inheriting methods through the hybrid form of the prototype chain. The basic idea behind
is: there is no need to call the constructor of the supertype in order to specify the prototype of the subtype. All we need is nothing more than a copy of the supertype
prototype. Essentially, you use parasitic inheritance to inherit the prototype of the supertype, and then assign the result to the prototype of the subtype
. The basic pattern of parasitic combinatorial inheritance is as follows.
function inheritPrototype(subType, superType){ var prototype = object(superType.prototype); //创建对象 prototype.constructor = subType; //增强对象 subType.prototype = prototype; //指定对象 }
个参数:子类型构造函数和超类型构造函数。在函数内部,第一步是创建超类型原型的一个副本。第二步是为创建的副本添加constructor 属性,从而弥补因重写原型而失去的默认的constructor 属性。最后一步,将新创建的对象(即副本)赋值给子类型的原型。这样,我们就可以用调用inheritPrototype()函数的语句,去替换前面例子中为子类型原型赋值的语句了。
集寄生式继承和组合继承的优点与一身,是实现基于类型继承的最有效方式。
以上就是JavaScript面向对象编程(继承实现方式)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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.

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

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

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.

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.
