prototype focus

Peter Michaux petermichaux at gmail.com
Thu Jun 30 19:17:19 PDT 2011


On Thu, Jun 30, 2011 at 4:46 PM, Axel Rauschmayer <axel at rauschma.de> wrote:
> What problem have you solved by giving the prototype a name?

The names I choose makes it clear to the reader that the prototype is
one element (zero) in a set of elements (complex numbers).

A name gives a reference to the object that is not related to any
constructor function. Constructor functions are something separate
from the prototype object (zero).


> The proposal is
> not (just) about giving prototypes names.

Understood but it seemed that the proposal started from the idea that
JavaScript needs to get back to its prototype roots. I don't think
JavaScript has ever been far from its prototype roots especially if
the programmer shifts to thinking about a prototype object instead of
thinking about a functions prototype property.


> Problems that both prototypes-as-classes (PAC) and class literals (CL) are
> trying to solve are:
> - Subclassing is hard and not directly supported by the language: connecting
> prototypes, chaining constructors, super-references.

Object.getPrototypeOf(this).foo.call(this) is pretty long.


> - Class properties are not inherited

Class property inheritance is definitely a secondary issue compared
with the instance objects themselves.

> (CoffeeScript copies them “manually”).

Which I think is unsatisfactory and has a lot of gotchas when some
properties are arrays and other properties have primitive values.

--

It seems to me that perhaps the PaC drifted too far or perhaps started
too far from what JavaScript has already. If the idea is to shift the
focus more towards prototypes, then starting from something like what
I've written and adding "super" syntax would be more consistent with
what JavaScript already has.

Peter


> If none of these issues are involved then today’s constructor functions are
> OK.
> Pros of each approach:
> (1) Pro CL: no need to support two kinds of classes: prototypes *and*
> constructor functions.
> (2) Pro PAC: look the same as class literals, but don’t need an extra
> translation step.
> (1) weighs heavily and will probably make it impossible to sell PAC to TC39.
> There is the precedent of Python having two styles of classes [1] at the
> same time, but I don’t think they diverged as heavily as constructor
> functions and PAC.
> If you want to argue against PAC benefits, take a look at Sect. 3 here:
> http://www.2ality.com/2011/06/prototypes-as-classes.html#3
> [1] http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python
> Axel
> On Jul 1, 2011, at 1:07 , es-discuss-request at mozilla.org wrote:
>
> From: Peter Michaux <petermichaux at gmail.com>
> Date: June 30, 2011 21:54:47 GMT+02:00
> To: es-discuss <es-discuss at mozilla.org>
> Subject: prototype focus
>
>
> There seems to have been a lot of fuss here about focusing on
> prototypes first rather than on constructor functions. As it stands
> now, I don't see how JavaScript makes focusing on prototypes
> difficult.
>
> // focus on the prototype first
> // Make it non-abstract.
> // Call it "zero" not "number".
> // (Another example: Call the object "adam" not "person".)
>
> var zero = {
>    realPart: 0,
>    imagPart: 0,
>    getMagnitude: function() {
>        return Math.sqrt(this.realPart * this.realPart +
>                         this.imagPart * this.imagPart);
>    }
> };
>
> // JavaScript makes it possible to have a variety of constructors
> // for objects that have zero as their prototype.
> // Yes the constructor property is gone. Is that actually a problem?
>
> function Real(r) {
>    this.realPart = r;
> }
> Real.prototype = zero;
>
> function Imaginary(i) {
>    this.imagPart = i;
> }
> Real.prototype = zero;
>
> function Complex(r, i) {
>    this.realPart = r;
>    this.imagPart = i;
> }
> Complex.prototype = zero;
>
> // Now make some objects.
>
> var two = new Real(2);
> var i = new Imaginary(2);
> var oneone = new Complex(1, 1);
>
> Isn't that prototype-focused enough?
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
> twitter.com/rauschma
> home: rauschma.de
> blog: 2ality.com
>
>
>


More information about the es-discuss mailing list