Typed properties beside Typed Objects
Andrea Giammarchi
andrea.giammarchi at gmail.com
Tue Mar 11 16:12:47 PDT 2014
Last thing, for backward compatible it means that these properties will
work regardless in ES5 and ES6 but without guarded types ... guard that has
been already implemented by my shim/experiment with these kind of
descriptors.
It's like adding `withCredentials` to XHR when it won't affect older
implementations (or anything new that is backward compatible, syntactically
speaking, and could be polyfilled somehow)
Hope I've explained better myself.
As quick recap: this is about typed properties for generic JS objects, not
only StructType/statically shaped one, if this makes any sense.
Regards
On Tue, Mar 11, 2014 at 4:04 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:
> 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/9fa1f861/attachment.html>
More information about the es-discuss
mailing list