www.webdeveloper.com
Results 1 to 6 of 6

Thread: prototype inheritance

  1. #1
    Join Date
    Apr 2010
    Posts
    26

    prototype inheritance

    Hey all,

    In this line:
    Code:
    function inherit(C, P) {
      var F = function () {};
      F.prototype = P.prototype;
      C.prototype = new F();
    }
    Someone said that:
    Code:
    The new inheriting function will actually separate the constructor of the parent from the child, because it's not pointing to the same object anymore, but now it's pointing to a prototype of a newly created function that has nothing to do with the parent. So, you actually create a local copy of the parent's constructor, and then create a new instance of the copy, which returns all constructor methods an properties. By changing the constructor of the child now, it will not affect the parent.
    My question is I don't understand how the child has nothing to do with the parent anymore when indeed we ASSIGN A REFERENCE of P prototype to F prototype. So F prototype must be pointing to P's prototype. When we instantiate F, the above quote says it contains a copy, but not a reference? apply() creates COPIES but object assignment creates REFERENCES so when assigning prototype, isn't a refeence, not a copy, created from P to F? And if it's a reference, then that link from C to F to P should still exist. But obviously it doesn't in the language so I am clearly missing something.

    Thanks for any clarification.

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    it's basically c.prototype.prototype=P.prototype .

    it doesn't copy, but the author is right that you can mod the child's prototype without affecting the parent's prototype.

    Code:
    function inherit(C, P) {
      var F = function () {};
      F.prototype = P.prototype;
      C.prototype = new F();
    }
    
    
    function A(){
     this.a=1;
    }
    
    function B(){
     this.b=2;
    }
    
    B.prototype.prop=true;
    
    inherit(A,B);
    
    var o=new A;
    
    o.prop=5;
    
    alert((new B()).prop)

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Or, in other words: JavaScript uses the delegation, not the inheritance. And that delegation is subsequent to a process of "cloning" the object, object which serves as a prototype.

    JavaScript is a prototype based language, not a class based one.
    Last edited by Kor; 11-09-2010 at 03:44 AM.

  4. #4
    Join Date
    Apr 2010
    Posts
    26
    I'm looking at this from memory position angle. Because F's prototype points to the same memory position as P's prototype and because prototype is an object and objects get passed around by reference so they reference same memory position during assignment.

    p _proto_ (memory position 001)
    F.prototype = P.prototype;
    F _proto_ (memory position 001)
    C.prototype = new F();
    C _proto_ (memory position 001)
    So if interpreter can't find property in C's _proto_, it looks up parent object F and checks if it has the _proto_ property and if it doesn't, then it searches P. Hence, C can inherit from P.

    But when you change C, and if C points to memory position 001, how can P not be affected, if P itself points to memory position 001?

    You mentioned it's like C.prototype.prototype = P.prototype;

    I don't see how the language interprets that prototype property has a property called prototype when it's not outright declared. All C's prototype does is point to an object, which has its own prototype.

    I've been stuck on understanding this for a week. Someone please explain why my memory position argument fails (despite the fact that objects passed by reference) and how one of the answers said that behind scenes a new prototype object is creared on C's prototype without ever being declared as an object. In reality, it's just a reference to an object.

    Confused.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by JohnMerlino View Post
    But when you change C, and if C points to memory position 001, how can P not be affected, if P itself points to memory position 001?
    C!=P. Changing C should have no effect on P. Changing P should have an effect on C. Remember, P is a property of C, reached by the special link .prototype. Consider:

    Code:
    var myProto={a:1};
    
      var F = function f() {};
      F.prototype = myProto;
      var C=  new F();
    
      var F2 = function f2() {};
      F2.prototype = C;
      var D = new F2();
    
    
    D.prototype==undefined
    D.prototype=34; //try to kill link
    D.a==1
    you can see that D reaches myProto, because D has an inherited .a prop that equals 1, just like myProto. Also notice that you can't see or touch the .prototype of the instance. This is because .prototype is a "special" property that executes a different internal getter than normal, "own", properties. It is marked "DontEnum", so there's no way you can see it from the js runtime, save a few bugs over the years.

    Quote Originally Posted by JohnMerlino View Post
    I don't see how the language interprets that prototype property has a property called prototype when it's not outright declared. All C's prototype does is point to an object, which has its own prototype.

    according to the spec, the "new" operator create a new empty object from ObjectObject. It then assigns another object to the new object's prototype property. Finally, it executes the code within the constructor to assign own properties to the new object. you should really read the ecmascript spec to get a firm handle on how "new" relates to "prototype".

  6. #6
    Join Date
    Apr 2010
    Posts
    26
    Thanks your explanation was the best.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles