


What are the inheritance methods of js? Introduction to several ways to implement inheritance in js
What this article brings to you is what are the inheritance methods of js? An introduction to several ways to implement inheritance in js has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. The way js implements inheritance: prototype chain
Implementation method: The instance of A prototype is the attribute of B prototype
No Forget that Object exists by default in the prototype chain
Adding methods to subclasses or overriding superclass methods must be placed after replacing the prototype statement
After inheritance is implemented through the prototype chain, object literals cannot be used Create methods and properties, because the prototype chain will be rewritten
After inheritance is implemented through the prototype chain, the reference type properties of the super class will be shared by all instances
function SuperType() { this.property = true; this.arr=[1,2,3] } SuperType.prototype.getSuperValue = function() { return this.property; } function SubType() { this.sub = false; } SubType.prototype = new SuperType(); //继承SuperType,即以superType的实例为中介,使subType。prototype指向superType的原型 SubType.prototype.getSubValue = function() { //添加新方法 return this.sub; } SubType.prototype.getSuperValue = function() { // 重写超类中的方法 return this.sub; } var instance1 = new SubType(); instance1.arr.push(4); console.log(instance1.arr); //1,2,3,4 var instance2 = new SubType(); console.log(instance2.arr); //1,2,3,4
2. js implementation inheritance Method: Borrow the constructor
Implementation method: Call the superclass constructor within the constructor of the subclass, that is, use call() or apply(), so that the scope of the superclass constructor changes.
You can pass parameters to the constructor, but you cannot reuse functions
function SuperType(name,age){ this.name = name; this.age = age; } function SubType() { SuperType.call(this,'i','21')//继承SuperType,并传递参数 this.job = 'actor' }
3. The way js implements inheritance: combined inheritance
Implementation method: Use the prototype chain to realize the inheritance of prototype properties and methods, and use the constructor to realize the inheritance of instance properties
Hidden danger: Call the parent class constructor twice (1call() method, 2new SuperType() )
function SuperType(name,age){ this.name = name; this.age = age; this.f = [1,2,3,4] } SuperType.prototype.sayName = function() { console.log(this.name) } function SubType(name,age) { SuperType.call(this,name,age)//继承SuperType,并传递参数 this.job = 'actor' } SubType.prototype=new SuperType(); SubType.prototype.constructor = SubType; SubType.prototype.sayHello=function() { console.log('hello') } var h = new SubType('hua', 18); h.sayName()//haha h.f.push(5) console.log(h.f)//1,2,3,4,5 var n = new SubType(); console.log(n.f)//1,2,3,4
4. The way js implements inheritance: prototypal inheritance
Based on an object, generate a new object, and then modify the new object
Attributes of super class reference types are still shared
var person = { name:'lily', age:'21', friends:[1,2,3] } var people = Object.create(person); people.friends.push(4); var human = Object.create(person); console.log(human.friends)//1,2,3,4
5. The way js implements inheritance: parasitic inheritance
Creation A function only used to implement the inheritance process, extend the object inside the function, and then return the object
At this time, the reference type attribute of the parent class is still shared by all instances
function anotherFunction(original) { var clone = Object(original); clone.sayHi = function() { console.log('hi') } return clone; } var person = { name:'lili', age:'21', f: [1,2,3] } var people1 = anotherFunction(person); people1.f.push(4) console.log(people1.f);// 1,2,3,4 var people2 = anotherFunction(person); console.log(people2.f);// 1,2,3,4
6. The way js implements inheritance: parasitic combined inheritance
Inherits properties by borrowing constructors, and inherits methods through prototype chain mixing
Reduces one parent During the execution of the class constructor, the attributes of the parent class reference type are not shared
function SuperType(name,age){ this.name = name; this.age = age; this.f = [1,2,3,4] } SuperType.prototype.sayName = function() { console.log(this.name) } function SubType(name,age) { SuperType.call(this,name,age) this.job = 'actor' } function inherit(superType,subType) { var property = Object.create(superType.property);// 创建父类原型的一个副本,并没有调用父类构造函数 property.constructor = subType;// 使父类原型副本的constructor属性指向子类 subType.property = property;// 子类的原型指向父类原型副本 } inherit(SuperType,SubType) var instance = new SubType('haha', 18); instance.sayName()//haha instance.f.push(5); console.log(instance.f);//1,2,3,4,5 var ins = new SubType(); console.log(ins.f);//1,2,3,4
Related recommendations:
JS Inheritance--Prototype Chain Inheritance and Class Inheritance_Basic Knowledge
Detailed explanation of examples of inheritance methods in JS
Several ways to implement inheritance in JS
The above is the detailed content of What are the inheritance methods of js? Introduction to several ways to implement inheritance in js. For more information, please follow other related articles on the PHP Chinese website!

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


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

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment
