JS inheritance usage example analysis_javascript skills
This article analyzes the usage of JS inheritance with examples. Share it with everyone for your reference. The specific analysis is as follows:
Inheritance: Subclasses do not affect the parent class. Subclasses can inherit some functions of the parent class (code reuse)
Inheritance of attributes: call the constructor of the parent class call
Method inheritance: for in: copy inheritance (jquery also uses copy inheritance extend)
1. Copy inheritance
function Person (name){ this.name = name; } Person.prototype.showName =function (){ alert(this.name); } function Worker(name,job){ Person.call(this,name); this.job = job; } extend(Worker.prototype, Person.prototype); //如果用Worker.prototype=Person.prototype的话,会造成引用相同的问题 function extend(obj1,obj2){ for(var i in obj2){ obj1[i] = obj2[i] } } var coder = new Worker('magicfly','frontEnd'); coder.showName();
2. Class inheritance
function Person (name){ this.name = name; } Person.prototype.showName =function (){ alert(this.name); } function Worker(name,job){ Person.call(this,name); this.job = job; } //Worker.prototype = new Person(); // 这样继承会继承父级的不必要属性 function F(){}; F.prototype = Person.prototype; Worker.prototype = new F(); //通过建立一个临时构造函数来解决 ,也称为代理函数 var coder = new Worker('MAGICFLY','START'); coder.showName();
3. Prototypal inheritance
var a = { name : '小明' }; var b = cloneObj(a); b.name = '小强'; //alert( b.name ); alert( a.name ); function cloneObj(obj){ var F = function(){}; F.prototype = obj; return new F(); }
Applicable conditions
Copy inheritance: Universal type, can be used with or without new
Class inheritance: new constructor
Prototypal inheritance: Object without new
I hope this article will be helpful to everyone’s JavaScript programming design.

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.

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

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

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.

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.

Usage of Transform in CSS The Transform property of CSS is a very powerful tool that can perform operations such as translation, rotation, scaling and tilting of HTML elements. It can dramatically change the appearance of elements and make web pages more creative and dynamic. In this article, we will introduce the various uses of Transform in detail and provide specific code examples. 1. Translate (Translate) Translate refers to moving an element a specified distance along the x-axis and y-axis. Its syntax is as follows: tran
