preface

Based on multiple searches of information, I concluded the book with my own understanding:

Constructor: a function that can be used to create an object of a particular type with the new keyword. For example, Object and Array are built-in native constructors that automatically appear in the execution environment at runtime and can be used directly. As follows:

var arr = new Array(); // Create an Array instance with the Array constructor arr arr[0]="a"; arr[1]="b"; alert(arr); //a,b var obj=new Object(); // Create an Object instance with the Object constructor obj obj.name="c"; obj.age=12; alert(obj.name); //cCopy the code

We can create a custom constructor with custom attributes and methods, such as:

Person function Person(name,age){this.name=name; this.age=age; this.sayName=function(){ alert(this.name)// }; Var person1=new Person("Tom",22); var person2=new Person("Jerry",21); person1.sayName(); //Tom person2.sayName(); //JerryCopy the code

Note the following points:

Constructor names always start with a capital letter (mainly to distinguish them from non-constructors, i.e. normal functions). Constructors are functions, and they are defined in the same syntax as normal functions. Constructors differ from normal functions in the way they are used. Any function called with the new operator can be used as a constructor. A normal function is called without the new operator

function Person(name,age){ this.name=name; this.age=age; this.sayName=function(){ alert(this.name)// }; } var person=new person ("Tom",22); person.sayName(); //Tom // Use Person("Jerry",30) as a normal function; // Add to window sayName(); //Jerry equals window.sayname ();Copy the code

Each function has a Prototype property, which is a pointer to the prototype object that is created at the same time the function is defined. The purpose of the prototype object is to contain properties and methods shared by all instances

Person.prototype.name="Tom"; person.prototype. name="Tom"; Person.prototype.age=25; Person.prototype.sayName=function(){ alert(this.name); }; // All attributes and methods in the prototype object are automatically shared by all instances. var person2=new Person(); person1.sayName(); //Tom person2.sayName(); //TomCopy the code

Whenever a new function is created, each function gets a prototype property that points to the function’s prototype object (which is created at the same time the function is defined), which in turn has a property called “constructor” that points to the function itself, achieving a kind of looping reference.

As in the above example: alert (Person) prototype) constructor = = = Person); // returns true

function Person(){} alert(Person.prototype.constructor===Person); //trueCopy the code

When a constructor is called to create a new instance, the instance will contain a pointer [[Prototype]] that points to the Prototype on which it was created. There is no standard way to access [[Prototype]]. But most browsers support access through __proto__.

Person.prototype.name="Tom"; person.prototype. name="Tom"; Person.prototype.age=25; Person.prototype.sayName=function(){ alert(this.name); }; // All attributes and methods in the prototype object are automatically shared by all instances. var person2=new Person(); person1.sayName(); //Tom person2.sayName(); //Tom alert(person1.__proto__===Person.prototype); //trueCopy the code

Using the example code above, the relationship between the objects is shown below:

Conclusion:

① Whenever a function is created, its prototype object is also created, and the properties and methods in the prototype object are shared by instances created through its corresponding constructor

After each function is created, it gets a prototype property that points to the prototype object of the function.

③ The __proto__ attribute of every new object refers to the prototype object of its constructor, that is, prototype.

4. Only new objects have a __proto__ attribute

5. When calling an attribute or method, the system searches for _PROtp_ until _PROtp_ of the Object Object is found. If there is no attribute or method, an error message is displayed

6. So adding properties and methods to constructor prototype is the same as adding properties and methods to __proto__ from new

It is because every new object’s _protp_ is equal to the constructor’s prototype content that properties and methods attached to prototype are available to child objects.

Well, the above is the whole content of this article, I hope the content of this article for everyone’s study or work has a certain reference learning value, if you have questions you can leave a message to exchange, thank you for your support of script home.