JavaScript Object-oriented learning the dynamics of four prototype objects

Source: Internet
Author: User

1, because the process of looking up values in a prototype is a search, any modifications we make to the prototype object can be immediately reflected from the instance---even if the instance was first created and the prototype is modified. The code is as follows:

function Person () {} var friend=New person (); Person.prototype.sayHello=function() {     alert ("Hello"// output: Hello


The above code first creates an instance of the person and saves it in a friend. The next code block adds a method SayHello () to the Person.prototype (person's prototype property pair object);

Based on the output, we found that even though a friend instance was created before adding a new method, it could still access the new method.

The reason can be attributed to the loose connection between the instance and the prototype, and when we call Friend.sayhello (), the property method named sayHello is first searched in the instance, and if not in the instance properties, the prototype will continue to be searched. The prototype and the instance are connected by a [[prototype]] pointer (the instance uses the [[prototype]] pointer to the prototype Property object to invoke the methods and properties in the prototype), because [[[prototype]] is a pointer, not a copy of the person, So even after the method of modifying (adding) The prototype has finished creating the instance, the instance can still access the prototype object after the modification is completed!

2, although using the above mentioned example and the connection loose connection relationship, we can at any time to add properties and methods for the prototype, and the modification can be reflected in all instances.

However, if you rewrite the entire prototype object, then the situation is different, in the previous essay said that when the call constructor creates an object instance, JS adds a [[prototype]] Pointer to the instance that points to the original prototype (that is, person.prototype). As in the following code, modifying the prototype to another object does not alter the value of the [[prototype]] pointer in the instance (indicating the white point, which is the instance or the original prototype);

Note: [[prototype]] in the instance only points to the original prototype;

function Person () {} var friend=New person (); Person.prototype={    Constructor:person,    name:"Zhang San", age    :    SayHello:function() {        alert ("Hello James");     // Error Console Error!

As we can see from the above figure, after overriding the prototype object, there is no change in the value of the [[prototype]] pointer of the object instance, it still points to the original prototype object.

JavaScript Object-oriented learning the dynamics of four prototype objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.