Home > Article > Web Front-end > 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);
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!