Must built-in prototypes also be valid instances? (Was: Why DataView.prototype object's [[Class]] is "Object"?)

Herby Vojčík herby at mailbox.sk
Tue Oct 2 10:52:26 PDT 2012



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. at FooBrand = true;
>     }
> }
> Foo.isFoo = function (obj) {return !!obj. at FooBrand};
>
>
> private @BarBrand;
> class Bar extends Foo {
>     constructor() {
>          super();
>          /* establish the internal Barness of the instance */
>          this. at BarBrand = true;
>     }
> }
> Bar.isBar = function (obj) {return !!obj. at 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. at FooBrand};
>
>     private @FooBrand;
>     class Foo {
>        constructor() {
>             /* establish the internal Fooness of the instance */
>             this. at FooBrand = true;
>        }
>     }
> }

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

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

> Allen

Herby


More information about the es-discuss mailing list