Detailed explanation of JS rewriting prototype objects
May 17, 2018 am 10:10 AMJSPrototype 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
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
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
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

How to convert MySQL query result array to object?

Simple JavaScript Tutorial: How to Get HTTP Status Code

What is the difference between arrays and objects in PHP?

How to get HTTP status code in JavaScript the easy way

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

Introduction to the new map of Genshin Impact version 4.4

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