Typed properties beside Typed Objects

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Mar 11 16:04:46 PDT 2014


Here an example based on partially typed prototype generating normal
dynamic instances with typed properties.

```javascript

var int32 = 0, string = '';

function UniqueIDGenerator(prefix, suffix) {
  this.i = 0;
  this.prefix = prefix || '';
  this.suffix = suffix || '';
}

Object.defineProperties(
  UniqueIDGenerator.prototype,
  {
    i: {
      writable: true,
      type: int32
    },
    prefix: {
      writable: true,
      type: string
    },
    suffix: {
      writable: true,
      type: string
    },
    new: {
      value: function () {
        this.i++;
        return this.toString();
      }
    },
    toString: {
      value: function () {
        return ''.concat(
          this.prefix,
          this.i,
          this.suffix
        );
      }
    }
  }
);

var uid = new UniqueIDGenerator('pre', 'suf');
'' + uid; // pre0suf
'' + uid.new(); // pre1suf

// dynamic shape
uid.whatever = 123; // OK

// guarded properties
uid.pre = 123; // throws new Error, pre is not string

```

Does this make any sense? It was a quick example but basically any
prototype you know in JS could have some property pre defined upfront with
a type ... imagine a Point2D "class" with also methods and not just x and y.

Regards




On Tue, Mar 11, 2014 at 10:37 AM, Brendan Eich <brendan at mozilla.com> wrote:

> Andrea Giammarchi wrote:
>
>> **Backward Compatible**
>>
>> Having an implicit default to `Any` means that this `type` descriptor
>> property could be simply ignored or implemented as _always Any_ in ES5 or
>> ES6 compatible engines but a simple library that wraps `Object.create`,
>>
>
> Still not making sense.
>
> If the types can be ignored, then they have no effect when evaluating new
> code as well as old. Anything else is not backward compatible.
>
> Please slow down and show what you want to happen in new code that differs
> because of a .type member of a property descriptor. Show a use-case.
>
> /be
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140311/ac85d450/attachment.html>


More information about the es-discuss mailing list