Detailed explanation of the steps for using js prototype objects
This time I will bring you a detailed explanation of the steps for using js prototypeobject, what are the notes for using js prototype object, the following is a practical case, let’s take a look .
Let’s start with a simpleConstructor Prototype object applet
function CreateObj( uName, uAge ) { this.userName = uName; this.userAge = uAge; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.showUserAge = function () { return this.userAge; }
There is no problem with this program, but it is very redundant. Every time a method is extended, a prototype object must be written. We can treat the prototype object prototype as a literal object, and all methods are in the literal object. Extension, can achieve the same effect:
CreateObj.prototype = { showUserAge : function(){ return this.userAge; }, showUserName : function(){ return this.userName; }, } var obj1 = new CreateObj( 'ghostwu', 22 ); var obj2 = new CreateObj( '卫庄', 24 ); console.log( obj1.showUserName(), obj1.showUserAge() ); //ghostwu 22 console.log( obj2.showUserName(), obj2.showUserAge() ); //卫庄 24
However, this way of writing the prototype object rewrites the default prototype object of CreateObj. The first problem caused is that the constructor no longer points to CreateObj.
Before rewriting, the constructor points to CreateObj
function CreateObj( uName, uAge ) { this.userName = uName; this.userAge = uAge; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.showUserAge = function () { return this.userAge; } console.log( CreateObj.prototype.constructor === CreateObj ); //true
After rewriting, the constructor points to Object
CreateObj.prototype = { showUserAge : function(){ return this.userAge; }, showUserName : function(){ return this.userName; }, } console.log( CreateObj.prototype.constructor === CreateObj ); //false console.log( CreateObj.prototype.constructor === Object ); //true
Therefore, the constructor cannot accurately identify the object because it will be modified
The programs we wrote before basically extended the methods on the prototype object (prototype) and then instantiated the object. Let's take a look. First, instantiate the object and then extend the function on the prototype object (prototype).
Can instance objects call extended methods normally?
function CreateObj( uName, uAge ) { this.userName = uName; this.userAge = uAge; } var obj1 = new CreateObj( 'ghostwu', 22 ); CreateObj.prototype.showUserName = function(){ return this.userName; } console.log( obj1.showUserName() ); //ghostwu
It can be called normally, but if the prototype object is overridden, it cannot be called
function CreateObj( uName, uAge ) { this.userName = uName; this.userAge = uAge; } var obj1 = new CreateObj( 'ghostwu', 22 ); CreateObj.prototype = { showUserName : function(){ return this.userName; } } console.log( obj1.showUserName() ); //报错
Because after the prototype object is rewritten, the instantiation occurs before the rewriting, so the implicit prototype proto of the instance will not point to the overridden prototype object, so another problem cannot be called. , if there is a reference type on the prototype object (prototype), be careful because multiple instances share the prototype object. As long as one instance changes the value of the reference type, all other instances will receive the changed results.
function CreateObj(){} CreateObj.prototype = { name : 'ghostwu', skills : [ 'php', 'javascript', 'linux' ] }; var obj1 = new CreateObj(); obj1.skills.push( 'python' ); var obj2 = new CreateObj(); console.log( obj2.skills ); //'php', 'javascript', 'linux', 'python'
The shared characteristics of prototype objects can easily extend some methods for some built-in objects, such as array deduplication
Array.prototype.unique = function(){ var res = []; for( var i = 0, len = this.length; i <p> I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website! </p><p>Recommended reading: </p><p><a href="http://www.php.cn/js-tutorial-393299.html" target="_blank">Detailed explanation of the steps for setting element styles in js</a><br></p><p><a href="http://www.php.cn/js-tutorial-393296.html" target="_blank">html canvas to implement screen capture</a><br></p><p><a href="http://www.php.cn/js-tutorial-393292.html" target="_blank">Detailed explanation of jQuery implementation of timer function</a><br></p><!--content end-->
The above is the detailed content of Detailed explanation of the steps for using js prototype objects. For more information, please follow other related articles on the PHP Chinese website!

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

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.


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

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

WebStorm Mac version
Useful JavaScript development tools

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

Atom editor mac version download
The most popular open source editor
