Loading ...
Sorry, an error occurred while loading the content.

[jslint] Re: top of inheritance chain

Expand Messages
  • Douglas Crockford
    ... prototype is a property of a constructor function, it is not the property through which an object delegates to another object, which is the key to
    Message 1 of 8 , Jul 30 1:49 PM
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
      >
      > On Thu, Jul 29, 2010 at 3:50 PM, Douglas Crockford
      > <douglas@...>wrote:
      >
      > > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
      > > Volkmann <r.mark.volkmann@> wrote:
      > > >
      > > > I'm a little confused about something that is pretty fundamental in
      > > > JavaScript. I was thinking that Object is a prototype of every object.
      > > > That doesn't seem to be the case though. For example, the following
      > > > code prints "false".
      > > >
      > > > var obj = {};
      > > > print(Object.isPrototypeOf(obj));
      > > >
      > > > So every object has the methods of Object, but there doesn't seem to
      > > > be a reflective way to see how that happens.
      > > >
      > > > Likewise, I think of all the wrapper classes like Boolean, Number and
      > > > String as "inheriting" from Object, but I can't verify that with code.
      > >
      > > {} inherits from Object.prototype.
      > >
      > > Object inherits from Function.prototype because it is a function.
      > >
      > > A string inherits from String.prototype, which inherits from
      > > Object.prototype.
      > >
      >
      > Thank you very much! That helped a lot!
      >
      > Can someone explain why the second line below outputs false?
      > I thought since the first line outputs true, the second would also.
      >
      > print(Object.prototype.isPrototypeOf(String.prototype)); // true
      > print(String.prototype.prototype === Object.prototype); // false; Why?

      prototype is a property of a constructor function, it is not the property through which an object delegates to another object, which is the key to inheritance. In some implementations, the delegation property is surfaced as __proto__, which is distinct from prototype.

      String.prototype is an object containing string methods. It is not a function, so it is likely that it does not have a .prototype property.

      String is a function, so it inherits from Function.prototype. String also has a prototype property containing string methods.

      The new operator makes a new object that inherits from the prototype property of a constructor function. I do not recommend use of the new operator.
    • Leonardo
      2010/7/30 Douglas Crockford (...) ... (...) Why not Doug? also, in order do emulate classes i ve been using closures like this:
      Message 2 of 8 , Jul 30 3:48 PM
      • 0 Attachment
        2010/7/30 Douglas Crockford <douglas@...>
        (...)

        > The new operator makes a new object that inherits from the prototype
        > property of a constructor function. I do not recommend use of the new
        > operator.
        >
        (...)

        Why not Doug?

        also, in order do emulate classes i've been using closures like this:

        function ClassX(a,b){

        this.method=function(x){
        //...
        }
        //...
        }


        and in order do emulate inheritance, i'm using some call methods:

        function ClassY(w){

        ClassX.call(this,2,w);
        //...
        }

        in this way which features do i lose?

        sorry for thread kindap and thanks for any advice.


        [Non-text portions of this message have been removed]
      • Douglas Crockford
        ... Read this: http://www.amazon.com/exec/obidos/ASIN/0596517742/wrrrldwideweb
        Message 3 of 8 , Jul 30 5:43 PM
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, Leonardo <sombriks@...> wrote:
          >
          > 2010/7/30 Douglas Crockford <douglas@...>
          > (...)
          >
          > > The new operator makes a new object that inherits from the prototype
          > > property of a constructor function. I do not recommend use of the new
          > > operator.
          > >
          > (...)
          >
          > Why not Doug?

          Read this: http://www.amazon.com/exec/obidos/ASIN/0596517742/wrrrldwideweb
        • Mark Volkmann
          ... Thanks for explaining that! I think I ve almost connected all the dots. function Foo() {} var foo = new Foo(); All of these line print true in Rhino:
          Message 4 of 8 , Aug 1, 2010
          • 0 Attachment
            On Fri, Jul 30, 2010 at 3:49 PM, Douglas Crockford <douglas@...>wrote:

            > > Can someone explain why the second line below outputs false?
            > > I thought since the first line outputs true, the second would also.
            > >
            > > print(Object.prototype.isPrototypeOf(String.prototype)); // true
            > > print(String.prototype.prototype === Object.prototype); // false; Why?
            >
            > prototype is a property of a constructor function, it is not the property
            > through which an object delegates to another object, which is the key to
            > inheritance. In some implementations, the delegation property is surfaced as
            > __proto__, which is distinct from prototype.
            >

            Thanks for explaining that! I think I've almost connected all the dots.

            function Foo() {}
            var foo = new Foo();

            All of these line print "true" in Rhino:

            print(foo.constructor === Foo);
            print(Foo.constructor === Function);
            print(Function.prototype.isPrototypeOf(Foo));
            print(Object.prototype.isPrototypeOf(Function.prototype));
            print(Foo.constructor.prototype === Function.prototype);

            Can someone explain why the last two lines print "false"?

            print(Function.constructor.prototype === Object.prototype);
            print(Function.constructor === Object);

            Maybe my assumption that all function objects inherit from Object.prototype
            is wrong.

            --
            R. Mark Volkmann
            Object Computing, Inc.


            [Non-text portions of this message have been removed]
          • pauanyu
            ... I dunno about Rhino, but in Chrome, Function.constructor.prototype is actually a function, that is constructed by Function. Try this:
            Message 5 of 8 , Aug 2, 2010
            • 0 Attachment
              > print(Function.constructor.prototype === Object.prototype);

              I dunno about Rhino, but in Chrome, Function.constructor.prototype is actually a function, that is constructed by Function.

              Try this:

              print(Function.constructor === Function.constructor.prototype.constructor);


              > print(Function.constructor === Object);

              At least in Chrome, Function's constructor is Function. Fun bit of recursion.

              Try this:

              print(Function.constructor === Function);


              I have no clue how the JavaScript interpreter handles that kind of infinite recursion, but it does.

              --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
              >
              > On Fri, Jul 30, 2010 at 3:49 PM, Douglas Crockford <douglas@...>wrote:
              >
              > > > Can someone explain why the second line below outputs false?
              > > > I thought since the first line outputs true, the second would also.
              > > >
              > > > print(Object.prototype.isPrototypeOf(String.prototype)); // true
              > > > print(String.prototype.prototype === Object.prototype); // false; Why?
              > >
              > > prototype is a property of a constructor function, it is not the property
              > > through which an object delegates to another object, which is the key to
              > > inheritance. In some implementations, the delegation property is surfaced as
              > > __proto__, which is distinct from prototype.
              > >
              >
              > Thanks for explaining that! I think I've almost connected all the dots.
              >
              > function Foo() {}
              > var foo = new Foo();
              >
              > All of these line print "true" in Rhino:
              >
              > print(foo.constructor === Foo);
              > print(Foo.constructor === Function);
              > print(Function.prototype.isPrototypeOf(Foo));
              > print(Object.prototype.isPrototypeOf(Function.prototype));
              > print(Foo.constructor.prototype === Function.prototype);
              >
              > Can someone explain why the last two lines print "false"?
              >
              > print(Function.constructor.prototype === Object.prototype);
              > print(Function.constructor === Object);
              >
              > Maybe my assumption that all function objects inherit from Object.prototype
              > is wrong.
              >
              > --
              > R. Mark Volkmann
              > Object Computing, Inc.
              >
              >
              > [Non-text portions of this message have been removed]
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.