Home > Web Front-end > Front-end Q&A > What is javascript prototype

What is javascript prototype

藏色散人
Release: 2021-11-11 17:21:57
Original
3023 people have browsed it

JavaScript objects have a built-in Prototype private property, which points to another object. We call this object the prototype of the original object.

What is javascript prototype

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is the prototype of javascript? (Including function: inheritance)

Learning objectives:
Know what js is a prototype, Prototype, constituent function, instance object relationship; prototype application scope.

What is a prototype
A function has a prototype. The function has an attribute called prototype. The prototype of the function points to an object. This object is called a prototype object. This prototype object has a constructor property that points to the function itself.

Instance objects also have prototypes. The prototype object that exists for instance objects is __proto__. __proto__ is generally called the invisible prototype. This invisible prototype There is a constructor property that points to the constructor that created the instance.


Constructor, instance object and prototype are directly related
Constructor Relationship with instance objects
There is also a constructor attribute in __proto__ in each instance object, which points to the constructor that created the instance:

The relationship between instance object __proto__ and constructor prototype
The __proto__ in each instance object points to the prototype in the constructor and the two are equal.

Constructor, prototype prototype object, instance object, proto, constructor, direct relationship

Prototype function

  • Prototype function one: data sharing, saving memory space
  • Prototype function two: to realize inheritance


How prototypes share data and save memory space
JavaScript object-oriented programming

How prototype implements inheritance
For example, there is now a constructor for the "animal" object.


#There is also a constructor for the "cat" object.


#How can we make "cat" inherit "animal"?
If the prototype object of "cat" points to an instance of Animal, then all instances of "cat" can inherit Animal.

The relationship diagram between cat1 instance object and constructor is as follows


 
Any prototype object has a constructor attribute pointing to its constructor.
Add Cat.prototype = new Animal(); after the code
Cat.prototype.constructor points to Animal.

The relationship diagram between cat1 instance object and constructor is as follows

Recommended study: "javascript basic tutorial"

The above is the detailed content of What is javascript prototype. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template