Classes: suggestions for improvement

Bob Nystrom rnystrom at google.com
Tue Jun 14 14:40:13 PDT 2011


On Tue, Jun 14, 2011 at 1:57 PM, felix <felix8a at gmail.com> wrote:

> On Tue, Jun 14, 2011 at 13:46, Bob Nystrom <rnystrom at google.com> wrote:
> > On Tue, Jun 14, 2011 at 1:34 PM, felix <felix8a at gmail.com> wrote:
> >>> Hm, by that argument "class" isn't a particularly good term either,
> >> since the thing created is not a class, it's a thing that generates
> >> objects that can be considered instances of a class.
> >
> > That's actually a pretty close definition of a class in other dynamic
> > languages. In Smalltalk, Ruby, and Python, classes are objects that have
> > methods to let you create instances of themselves.
>
> Then what's the problem with calling it a "proto" instead of a
> "class"?


Sorry, I probably wasn't clear before. What I was getting at is that the
thing that "class" creates *is* pretty much like a class in other dynamic
languages. If you do this in JS:

class Point {}


The end result is a Point object that works very much like a "class" in
Python, Ruby, or Smalltalk. From that angle "class" is a pretty good keyword
to use.


>  Ok, it's not the same thing as x.prototype or x.__proto__,
> but this doesn't seem to me like it would be a big source of
> confusion, and it seems to me less confusing than "class" baggage.
>

Prototypes are already a large source of confusion in JS. They're an
uncommon feature in programming languages, and JS's use of them makes them
more obscure.

Consider:

function Point(x, y) { this.x = x; this.y = y; }
var p = new Point(1, 2);


Ask many JS programmers what p's prototype is and they'll say Point. You and
I know it's actually Point.prototype. Ask if p "inherits" from Point and
many will say "yes". JS uses "prototype" to mean two distinct things: 1) the
object associated with a constructor function that's used as the
prototypical instance of objects created by that constructor and 2) the
object an object inherits from. (Self uses "parent" for the latter, which
makes things less confusing.)

Using "proto" to define a constructor would make that even murkier. If I
did:

proto Point(x, y) { this.x = x; this.y = y; }
var p = new Point(1, 2);


That implies (to me at least) that Point is a "prototype" (which it of
course isn't, since nothing is inheriting from it at all). I fear then
people would start thinking "prototype" means the constructor object itself.
Eek!

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


More information about the es-discuss mailing list