JavaScript prototype and inheritance description_js object-oriented
function A(x) { this.x = x; } alert(A.prototype); alert(A.prototype.constructor);
According to the pop-up results, we can get: the prototype object is created by the constructor of the function. The properties it possesses can be shared by all objects. Initially, the prototype object points to an Object object and defines a constructor attribute. The attribute points to the constructor itself that defines the prototype object. Look at the following code again.
Code
function A(x) { A.prototype.x = x; } var obj = new A(10); alert(obj.x); alert(obj.constructor);
Because all properties of the prototype object can be shared by the object created by the constructor, the created object can access the constructor property here. At the same time, obj corresponds to an instance created by the prototype object (prototype), then override the constructor property. What will be the result?
Code
function A(x) { A.prototype.x = x; } var objA = new A(10); objA.constructor = function(x){ alert("重写obj的constructor属性");this.x = 20 }; objA.constructor(); alert(objA.x); var objB = new A(10); alert(objB.x);
According to the results, we can see that the first thing that pops up is "Rewrite the constructor attribute of obj", then 20 pops up, and 10 pops up. It can be seen that after we write and rewrite the constructor of the objA object, objB has not been changed. , so no matter how many attributes are added or modified to an object, it will not affect the original appearance of the attributes in its prototype object. In fact, it is easy to understand why js does this, because the behavior of one object cannot affect other objects, otherwise it will cause confusion.
Here, we can summarize the rules of the above code:
1. When we call an object, we first check the self-defined properties of the object itself and call it if it exists.
2. When its own properties do not exist, the reference of the prototype object defined by its constructor is called.
Then according to this rule, a prototype chain in JavaScript is formed, and we can define the inheritance relationship according to this rule.
function A(x) { A.prototype.x = x; } function B(x,y) { B.prototype.y = y; A.call(this,x); }
This code shows two functions. The B function inherits the A function. A.call(this.x) means passing the B object this to the A function for execution. Then, we also need the object constructed by the B function to contain all the characteristics of the A function, so we need to add this sentence.
B.prototype = new A();
alert(B.prototype.constructor);
First, we specify that the prototype of B is A. Therefore, the B function inherits the characteristics of the A function. According to the pop-up results, we can see that its constructor points to the A function, so are the characteristics of our B function lost? ? Therefore, we need to add a sentence and finally give the continued integration code.
function A(x) { A.prototype.x = x; A.prototype.ShowA = function(){ alert("A的Show方法!"); }; } function B(x,y) { B.prototype.y = y; A.call(this,x); B.prototype.ShowB = function(){ alert("B的Show方法!"); }; } B.prototype = new A(); B.prototype.constructor = B; var obj = new B(10,5); alert(obj.x); alert(obj.y); obj.ShowA(); obj.ShowB();
If you have any questions or errors, please feel free to make corrections and discuss them.

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

Introducing the new map of Genshin Impact version 4.4. Friends, Genshin Impact 4.4 version also ushered in the Sea Lantern Festival in Liyue. At the same time, a new map area will be launched in version 4.4 called Shen Yu Valley. According to the information provided, Shen Yugu is actually part of Qiaoying Village, but players are more accustomed to calling it Shen Yugu. Now let me introduce the new map to you. Introduction to the new map of Genshin Impact version 4.4. Version 4.4 will open "Chenyu Valley·Shanggu", "Chenyu Valley·Nanling" and "Laixin Mountain" in the north of Liyue. Teleportation anchor points have been opened for travelers in "Chenyu Valley·Shanggu" . ※After completing the prologue of the Demon God Quest·Act 3: The Dragon and the Song of Freedom, the teleportation anchor point will be automatically unlocked. 2. Qiaoyingzhuang When the warm spring breeze once again caressed the mountains and fields of Chenyu, the fragrant

Prototype, an object in js, is used to define the properties and methods of other objects. Each constructor has a prototype attribute. This attribute is a pointer pointing to a prototype object. When a new object is created, the new object will be The prototype attribute of its constructor inherits properties and methods. Prototype chain, when trying to access the properties of an object, js will first check whether the object has this property. If not, then js will turn to the prototype of the object. If the prototype object does not have this property, it will continue to look for the prototype of the prototype.

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

C++ function inheritance should not be used in the following situations: When a derived class requires a different implementation, a new function with a different implementation should be created. When a derived class does not require a function, it should be declared as an empty class or use private, unimplemented base class member functions to disable function inheritance. When functions do not require inheritance, other mechanisms (such as templates) should be used to achieve code reuse.
