Setting __proto__

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • limweizhong
    New Member
    • Dec 2006
    • 62

    Setting __proto__

    I am confused as to why __proto__ is now deprecated and something less flexible - getPrototypeOf - is recommended. Having a __proto__ property will allow you to set the object "type"/methods of a currently existing object, which cannot be achieved with Object.getProto typeOf and Object.create. One application that I can think of is if you have a drawing tool, and you want to allow users to switch between shapes with similar parameters but display differently, e.g. rectangle, rounded rectangle, parallelogram, etc. (like Microsoft Office Word). Then it would be good to have a display function for each "type" of shape, and you switch the shape by changing the __proto__ property. Would there be any way to do this?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I am confused as to why __proto__ is now deprecated
    because it’s non-standard and JavaScript is complicated enough with its browser-specific extensions.

    besides that Object.getProto typeOf() serves a totally different purpose (direct access to the prototype chain, which is not possible otherwise from the object instances)

    One application that I can think of is if you have a drawing tool, and you want to allow users to switch between shapes with similar parameters but display differently, e.g. rectangle, rounded rectangle, parallelogram, etc. (like Microsoft Office Word). Then it would be good to have a display function for each "type" of shape, and you switch the shape by changing the __proto__ property. Would there be any way to do this?
    make a method for each shape. you may use other methods inside that method to accomplish common tasks. you can also use a Decorator Pattern (or use inheritance).

    besides that you should never touch the inheritance chain to influence an instance method’s behaviour (you can, of course, define an object property for type, which you use in the drawing method)

    Comment

    • limweizhong
      New Member
      • Dec 2006
      • 62

      #3
      Isn't Object.getProto typeOf(obj) just going to be a reference to the obj.__proto__ object?

      I thought making methods for each shape would defeat the purpose of object-oriented programming? I suppose what you mean is to have a super-shape that has a shape type, and the display method would have to "switch case" for each type, and that is not really good when you could have done it with different classes that inherit a parent class (shape) having some base methods such as line/fill style. But if the user wants to "Change Autoshape", without the __proto__ switching you would have to create a new object and copy everything to it.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Isn't Object.getProto typeOf(obj) just going to be a reference to the obj.__proto__ object?
        nope. getPrototypeOf( ) accesses the prototype property.

        MDC about __proto__
        Description

        When an object is created, its __proto__ property is set to constructing function's prototype property. For example var fred = new Employee(); will cause fred.__proto__ = Employee.protot ype;.

        This is used at runtime to look up properties which are not declared in the object directly. E.g. when fred.doSomethin g() is executed and fred does not contain a doSomething, fred.__proto__ is checked, which points to Employee.protot ype, which contains a doSomething, i.e. fred.__proto__. doSomething() is invoked.

        Note that __proto__ is a property of the instances, whereas prototype is a property of their constructor functions.

        Comment

        • limweizhong
          New Member
          • Dec 2006
          • 62

          #5
          Sorry, but I think you are really wrong about this. prototype is a property of constructor functions, while Object.getProto typeOf(obj) will get the prototype link of obj.

          Comment

          • limweizhong
            New Member
            • Dec 2006
            • 62

            #6
            To make it clear, x.prototype is not equal to Object.getProto typeOf(x), even if x is a function. Object.getProto typeOf(function Square(length){ this.length=len gth; }) will return Function.protot ype and not Square.prototyp e.

            Try this:
            Code:
            function Square(length){ this.length=length; }
            alert(Object.getPrototypeOf(Square)==Function.prototype);
            alert(Object.getPrototypeOf(Square)==Square.prototype);

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Sorry, but I think you are really wrong about this. prototype is a property of constructor functions, while Object.getProto typeOf(obj) will get the prototype link of obj.
              let me correct myself: getPrototypeOf( ) accesses the internal [[Prototype]] property. (cf. ECMAScript 262 5.1 sections 15.2.3.2 & 8.6.2)

              Comment

              • limweizhong
                New Member
                • Dec 2006
                • 62

                #8
                So what I was saying was that __proto__ is really what's retrieved by Object.getProto typeOf() but there is no Object.setProto typeOf(). And I heard the original "recommende d" way to have inheritance was using:
                Code:
                NewClass.prototype.__proto__=SuperClass.prototype;
                So now I suppose it is:
                Code:
                NewClass.prototype=Object.create(SuperClass.prototype);

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  So what I was saying was that __proto__ is really what's retrieved by Object.getProto typeOf() but there is no Object.setProto typeOf(). And I heard the original "recommende d" way to have inheritance was using:
                  Code:
                  NewClass.prototype.__proto__=SuperClass.prototype;
                  actually, I’ve never seen extend code to use __proto__ before.

                  Comment

                  Working...