Home > Web Front-end > JS Tutorial > body text

What does js prototype mean?

藏色散人
Release: 2020-09-16 17:46:05
Original
15162 people have browsed it

What does js prototype mean?

Prototype is a difficult concept to understand in JavaScript. There are many attributes related to prototype. Objects have "prototype" attributes, function objects have "prototype" attributes, and prototype objects have " constructor" property.

First introduction to prototype

In JavaScript, the prototype is also an object. The property inheritance of the object can be realized through the prototype. JavaScript objects all contain a "[[ Prototype]]" internal attribute, this attribute corresponds to the prototype of the object.

"[[Prototype]]" as an internal property of the object cannot be directly accessed. So in order to conveniently view the prototype of an object, Firefox and Chrome provide the non-standard (not supported by all browsers) __proto__ accessor (ECMA introduced the standard object prototype accessor "Object.getPrototype(object)") .

In JavaScript, the prototype object also contains a "constructor" attribute, which corresponds to the constructor that creates all instances pointing to the prototype.

In JavaScript, each function has a prototype attribute, when a function is used as a constructor to create an instance, the prototype attribute value of this function will be assigned to all object instances as a prototype (that is, setting the `__proto__` attribute of the instance), that is, the value of all instances The prototype refers to the prototype attribute of the function. (****`Only function objects will have this attribute!`****)

The process of new is divided into three steps

var p = new Person('张三',20);
Copy after login

1. var p={}; Initialize an object p.

2. p._proto_=Person.prototype;, set the __proto__ attribute of object p to Person.prototype

3. Person.call(p,"Zhang San" ,20);Call the constructor Person to initialize p.

The above is the detailed content of What does js prototype mean?. 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