Symbol.hasInstance and [[Get]] vs [[GetOwnProperty]]
/#!/JoePea
joe at trusktr.io
Wed Aug 24 01:35:52 UTC 2016
Well, I do think this is fragile, because a class constructor should be
checking if an instance is an instance created by said constructor, and the
logic should not have to worry about super and sub class Function behavior
like that, requiring needless boilerplate conditional code in every
hasInstance function.
Symbol.hasInstance could have been spec'd differently, for example it could
just be a static property without inheritance (using
`[[GetOwnProperty]]`?). I don't think there would be anything wrong with
that, and it would work nicer in my opinion, and it would *still* be
possible to call super if you really really wanted to by simply referencing
the super constructor directly.
I am willing to bet that in most cases people will not need inheritance in
the implementation of a `hasInstance` function, and will find the needed
boilerplate code unwanted.
> if you want to revert to the default behavior set the value of B or C’s
[Symbol.hasInstance] own property to undefined.
That just causes errors. In my environment (Babel),
```js
Object.defineProperty(SomeClass, Symbol.hasInstance, {
value: undefined
})
// ...
someClass instanceof SomeClass // error
```
results in
```js
Uncaught TypeError: Cannot read property 'call' of undefined
```
At the top of all my `hasInstance` functions so far, I have boilerplate
code like the following, and it is not likely that there will be a case
where I don't want such boilerplate code:
```js
class SomeClass extends SomeOtherClass {}
Object.defineProperty(SomeClass, Symbol.hasInstance, {
value: function(obj) {
if (this !== SomeClass) return
Object.getPrototypeOf(SomeClass)[Symbol.hasInstance].call(this, obj) //
needed boilerplate
// ... instance-checking logic ...
}
})
// or, using `static`:
class SomeClass extends SomeOtherClass {
static [Symbol.hasInstance](obj) {
if (this !== SomeClass) return super[Symbol.hasInstance](obj) //
needed boilerplate
// ... instance-checking logic ...
}
}
```
It seems somehow wrong to require that boilerplate code.
I can live with it though, now that I know how it works.
*/#!/*JoePea
On Tue, Aug 16, 2016 at 10:37 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:
>
> > On Aug 16, 2016, at 9:56 AM, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
> >
> > fwiw I agree with Jason thanks gosh inheritance worked as expected even
> for public statics.
> >
> > If you want to overwrite the inherited behaviour just do that on class B
> and eventually C.
> >
> And if you want to revert to the default behavior set the value of B or
> C’s [Symbol.hasInstance] own property to undefined.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160823/fd383d60/attachment.html>
More information about the es-discuss
mailing list