Home Web Front-end JS Tutorial Detailed explanation of JS rewriting prototype objects

Detailed explanation of JS rewriting prototype objects

May 17, 2018 am 10:10 AM
javascript prototype object

JSPrototype modification and rewriting

There are two ways to modify the js prototype:

1. In the original prototype Add properties and methods to the object: 12

function Person(){
            }
            Person.prototype.name="Mike";
            Person.prototype.sayName=function(){
                console.log(this.name);
            }            var person=
new
 Person();
            person.sayName(); //Mike123456789
Copy after login

2. Override or overwrite the prototype object: 12

  function Person(){
            }
            Person.prototype={                "name":"Mike",
                sayName:function(){
                    console.log(this.name);
                }
            }            var person=new Person();
            person.sayName(); //Mike1234567891011
Copy after login

Next let’s look at a question: (This question also explains the difference between adding properties and methods directly to the prototype object and overriding or overwriting the prototype object.) 12
       

function Person(){
            }            function Animal(){
            }            var person=new Person();            var animal=new Animal();
            Person.prototype={                "name":"Mike",
                sayName:function(){
                    console.log(this.name);
                }
            }
            Animal.prototype.name="animal";
            Animal.prototype.sayName=function(){
                console.log(this.name);
            }
            person.sayName(); //error person.sayName is not a function
            animal.sayName();//animal1234567891011121314151617181920
 分析:12
            function Person(){
            }            function Animal(){
            }            var person=new Person();            var animal=new Animal();
            console.log(person.
proto
===Person.prototype); //true
            console.log(animal.proto===Animal.prototype); //true
            Person.prototype={                "name":"Mike",
                sayName:function(){
                    console.log(this.name);
                }
            }
            Animal.prototype.name="animal";
            Animal.prototype.sayName=function(){
                console.log(this.name);
            }
            console.log(person.proto===Person.prototype); //false
            console.log(animal.proto===Animal.prototype); //true
            person.sayName(); //error person.sayName is not a function
            animal.sayName();//animal
Copy after login

Above This is a JS rewriting prototype object that I compiled for you. I hope it will be helpful to you in the future.

Related articles:

Detailed explanation of rewriting and overloading techniques of js methods

Focus on analysis of JavaScript rewriting alert () method skills

Focus on method rewriting in js inheritance

The above is the detailed content of Detailed explanation of JS rewriting prototype objects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

What is the Request object in PHP?

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

How to convert MySQL query result array to object?

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

What is the difference between arrays and objects in PHP?

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

What should I pay attention to when a C++ function returns an object? What should I pay attention to when a C++ function returns an object? Apr 19, 2024 pm 12:15 PM

What should I pay attention to when a C++ function returns an object?

Introduction to the new map of Genshin Impact version 4.4 Introduction to the new map of Genshin Impact version 4.4 Jan 31, 2024 pm 06:36 PM

Introduction to the new map of Genshin Impact version 4.4

Analyze the differences between heap and stack in Java and their application scenarios Analyze the differences between heap and stack in Java and their application scenarios Feb 24, 2024 pm 11:12 PM

Analyze the differences between heap and stack in Java and their application scenarios

See all articles