Whose priority is to access the property value

1. Rewrite the prototype object with a default prototype object comparison

  • Rewrite the prototype object equivalent to a new space, and the default prototype object independent of each other
  • Recent examples point to the prototype object from the time it is created.
        function Person(){
        }

        Person.prototype = {
            constructor: Person,
            name : "Nicholas",
        };
        
        Person.prototype.name="Betty";

         var friend = new Person();      
        alert(friend.name);   //Betty
        function Person(){
        }
        Person.prototype.name="Betty";

        Person.prototype = {
            constructor: Person,
            name : "Nicholas",

        };

         var friend = new Person();       
        alert(friend.name);   //Nicholas

Description: create an instance of the Person, and then assigned to the default prototype and rewrite its prototype object. When a friend of code is created from the default prototype near, so friend point to the default prototype. And when the two friend codes to be created near the prototype rewrite, rewrite friend pointed prototype.

Example 2. Comparison of the same name and the prototype attributes

        function Person(){
        }

        
        Person.prototype.name="Betty";
         var friend = new Person();
        friend.name="Kitty";
        alert(friend.name);   //Kitty

Explanation: The instance properties must perform the normal follow after an instance is created.

        function Person(){
        }
        friend.name="Kitty";

        Person.prototype = {
            constructor: Person,
            name : "Nicholas",
        };
        
        Person.prototype.name="Betty";
         var friend = new Person();

        alert(friend.name);   //无结果

Published an original article · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/u011927449/article/details/104033261