Home  >  Article  >  Web Front-end  >  What is the javascript prototype property? and the three steps to instantiate an object

What is the javascript prototype property? and the three steps to instantiate an object

伊谢尔伦
伊谢尔伦Original
2017-07-20 15:42:451469browse

All JS functions have a prototype attribute, which refers to an object, the prototype object, also referred to as the prototype. This function includes constructors and ordinary functions. We are talking more about the prototype of the constructor, but we cannot deny that ordinary functions also have prototypes. For example, ordinary functions:

function F(){ 
  alert(F.prototype instanceof Object) //true; 
}

Constructor, that is, constructing objects. First, let’s understand the process of instantiating an object through the constructor.

function A(x){ 
  this.x=x; 
} 
var obj=new A(1);

There are three steps to instantiate the obj object:
1. Create the obj object: obj=new Object();

2. The internal __proto__ points to the prototype of the function A that constructed it. At the same time, obj.constructor===A.prototype.constructor (this is always true, even if A.prototype no longer points to the original A prototype, that is to say: class The constructor property of the instance object always points to the prototype.constructor of the "constructor"), so that obj.constructor.prototype points to A.prototype (obj.constructor.prototype===A.prototype, which does not hold when A.prototype changes , encountered below). obj.constructor.prototype and the internal _proto_ are two different things. _proto_ is used when instantiating an object. obj does not have a prototype attribute, but it has an internal __proto__. You can use __proto__ to obtain the prototype attributes on the prototype chain. and prototype methods, FireFox exposes __proto__, which can be alerted (obj.__proto__) in FireFox;

3. Use obj as this to call constructor A to set members (i.e. object properties and object methods) and initialized.
When these three steps are completed, the obj object has no connection with the constructor A. At this time, even if the constructor A adds any members, it will no longer affect the instantiated obj object. At this time, the obj object has the x attribute and all members of the prototype object of constructor A. Of course, the prototype object has no members at this time.

The prototype object is initially empty, that is, it does not have a member (ie prototype properties and prototype methods). You can verify how many members a prototype object has by using the following method.

var num=0; 
for(o in A.prototype) { 
  alert(o);//alert出原型属性名字 
  num++; 
} 
alert("member: " + num);//alert出原型所有成员个数。

However, once prototype properties or prototype methods are defined, all objects instantiated through the constructor inherit these prototype properties and prototype methods through the internal _proto_ chain to achieve.

A.prototype.say=function(){alert("Hi")};

Then all objects of A have a say method. The say method of this prototype object is the only copy shared by everyone, instead of every object having a copy of the say method.

The above is the detailed content of What is the javascript prototype property? and the three steps to instantiate an object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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