


Sharing the differences between three JavaScript simulation implementation encapsulation methods and their writing methods
Inheritance is to use a subclass to inherit another parent class, then the subclass can automatically have all the properties and methods in the parent class. This process is called inheritance! There are many ways to implement inheritance in JS. Today I will introduce three of them to you. Let’s use this article to learn about the three methods of encapsulation through js simulation. Friends who need it can refer to it. I hope it can help everyone.
1. Add an extension method to the Object class
//声明一个父类 function Person(name){ this.name=name; this.age=age; this.say=function(){ alert("我叫"+this.name); } } //声明一个子类 function Student(){ this.no=no; this.study=function(){ alert("我在学习!"); } } // 通过循坏,将父类对象的所有属性和方法,全部赋给子类对象 Object.prototype.extend=function(parent){ for(var i in parent){ this[i].parent[i]; } } var p=new Person("张三",12); var s=new Student("1234567"); //子类对象调用这个扩展方法 s.extend() console.log(s);
The above-mentioned principle of inheritance:
Through looping, all properties and methods of the parent class object are Assigned to subclass object. The key point is the for-in loop. Even without extending Object, the operation can be implemented through a simple loop.
However, there are some disadvantages in using this method to implement inheritance:
① You cannot directly get the complete subclass object through one instantiation. You need to get the parent class object and the subclass object first, and then merge them manually;
②The inheritance method of extending Object will also be retained on the object of the subclass.
Let’s take a look at the second method of implementing inheritance~
2. Use prototypal inheritance
Before introducing this method, let’s talk about two concepts: prototype objects And prototype
1. prototype: the prototype object of the function
①Only functions have prototypes, and all functions must have prototypes
②The prototype itself is also an object!
③prototype points to the reference address where the current function is located!
2. __proto__: prototype of the object!
①Only objects have __proto__, and all objects must have __proto__
②__proto__ is also an object, so it also has its own __proto__. Follow this line in the order of upwards. It's the prototype chain.
③ Functions and arrays are objects and have their own __proto__
//声明父类 function Person(name,age){ this.name=name; this.age=age; this.say=function(){ alert("我叫"+this.name); } } //声明子类 function Student(no){ this.no=no; this.study=function(){ alert("我在学习!我叫"+this.name+"今年"+this.age"); } } //将父类对象赋给子类的prototype Student.prototype=new Person("张三",14); //拿到子类对象时,就会将父类对象的所有属性和方法,添加到__proto__ var s=new Student(); s.study();
The principle of using prototype inheritance:
Assign the parent class object to the prototype of the subclass , then the attributes and methods of the parent class object will appear in the prototype of the subclass. Then, when instantiating a subclass, the prototype of the subclass will be in the __proto__ of the subclass object. Finally, the attributes and methods of the parent class object will appear in the __proto__ of the subclass object.
Characteristics of this kind of inheritance:
①All properties of the subclass itself are member properties, and properties inherited from the parent class are prototype properties.
② Still cannot get the completed subclass object through one-step instantiation.
The third method to implement inheritance:
call(), apply() and bind(), these three methods are very similar, only different in passing parameters.
function Person(name,age){ this.name=name; this.age=age; this.say=function(){ alert("我叫"+this.name); } } function Student(no,name,age){ this.no=no; this.study=function(){ alert("我在学习!"); } //将父类函数的this,指向为子类函数的this Person.call(this,name,age); } var s=new Student(12,"张三",24); console.log(s);
The only difference between the three functions is the way they accept the parameter list of func. Other than that, there is no difference in functionality!
How to write the three functions (difference):
How to write call: func.call(obj pointed to by this of func, func parameter 1, func parameter 2,...);
Apply writing method: func.apply(obj pointed by this of func, [func parameter 1, func parameter 2,...]);
Bind writing method: func.bind(this pointed by func obj)(func parameter 1, func parameter 2,...);
Related recommendations:
Detailed explanation of a Vue plug-in from encapsulation to release
The encapsulation function in jquery passes the current element
The above is the detailed content of Sharing the differences between three JavaScript simulation implementation encapsulation methods and their writing methods. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
