prototype focus

Axel Rauschmayer axel at rauschma.de
Thu Jun 30 16:46:01 PDT 2011


What problem have you solved by giving the prototype a name? The proposal is not (just) about giving prototypes names.

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.
- Class properties are not inherited (CoffeeScript copies them “manually”).

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



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110701/8b195785/attachment-0001.html>


More information about the es-discuss mailing list