Allen Wirfs-Brock wrote:
I think we have all the language features need to do reliable
branding by ES programmers where they need it. We just need to
establish the patterns for doing that. Here is the one I propose:

private @FooBrand;
class Foo {
    constructor() {
         /* establish the internal Fooness of the instance */
         this.@FooBrand = true;
    }
}
Foo.isFoo = function (obj) {return !!obj.@FooBrand};


private @BarBrand;
class Bar extends Foo {
    constructor() {
         super();
         /* establish the internal Barness of the instance */
         this.@BarBrand = true;
    }
}
Bar.isBar = function (obj) {return !!obj.@BarBrand};

Note that an instance of Bar will be true for both Foo.isFoo and Bar.isBar

This pattern is fine as long as it is ok that anything processed by
the Foo or Bar constructor gets branded because not, anybody can do:
    let myFoo = Foo.call({ });

If you really need to strongly tie instantiation with branding you
probably have to use a factory function:

module Fooishness {
    export function FooFactory ( ){return  new Foo};
    FooFactory.isFoo = function (obj) {return !!obj.@FooBrand};

    private @FooBrand;
    class Foo {
       constructor() {
            /* establish the internal Fooness of the instance */
            this.@FooBrand = true;
       }
    }
}

var iWillBeFoo = {};
Fooishness.FooFactory().constructor(iWillBeFoo);

In fact, it has its logic to `newFoo.@FooBrand = true;` in factory, which solves it, hopefully cleanly enough.

Allen

Herby
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to