Summary: prototypes as classes

Bob Nystrom rnystrom at google.com
Tue Jun 28 17:28:33 PDT 2011


On Tue, Jun 28, 2011 at 5:19 PM, Axel Rauschmayer <axel at rauschma.de> wrote:
As an aside:
- Static methods in Java always felt like a hack, because Java’s creators
couldn’t bring themselves to support first-class functions.
- Thus: I would much prefer to put Object.* methods into a module (once we
have those). The same holds for “class methods” such as Math.*. There Math
is used for namespacing and not really a class.

+1 for that.

> However: Prototypes are already *the* core mechanism with regard to JS
inheritance, they are what stays after an instance has been created, they
are actually used for instanceof tests, etc.

But before you've created an instance, it is pretty handy to have the
constructor bound to a name so you can get to it. I don't think making the
named object be the constructor means that they're *more* important, just
that you generally have to go through them *first* to get to an instance.
They get the name because they're the entrypoint.

   // Superclass
   var Person = {
       constructor: function (name) {
           this.name = name;
       },
       describe: function() {
           return "Person called "+this.name;
       }
   };

   // Subclass
   var Worker = Person <| {
       constructor: function (name, title) {
           Person.constructor.call(this, name);
           this.title = title;
       },
       describe: function () {
           return Person.describe.call(this)+" ("+this.title+")"; // (*)
       }
   };

For comparison's sake, here's that in the class proposal:

    class Person {
      constructor(name) {
        this.name = name;
      }
      describe() {
        return "Person called " + this.name;
      }
    }

    class Worker extends Person {
      constructor(name, title) {
        super(name);
        this.title = title;
      }
      describe() {
        return super.describe() + " (" + this.title + ")";
      }
    }

I definitely sympathize with the purity/simplicity argument against adding
class syntax to JS, but for me, that code does read better with a dedicated
declarative class syntax.

- bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110628/3d961893/attachment.html>


More information about the es-discuss mailing list