The JavaScript class syntax added in ES6 seems to make it legal for extend null :
class foo extends null {}
Some Googling reveals that he suggested on ES Discuss that such announcements should be made a mistake; however, other commentators claimed that they were left legitimate on the grounds that
someone might want to create a class with the prototype {__proto__: null}
and this side of the argument ultimately prevailed.
I cannot fully understand this hypothetical use case. First, although declaring such a class is legal, it seems that creating an instance of a class declared in this way is not. Trying to instantiate foo class on top in Node.js or Chrome gives me a wonderful error
TypeError: function is not a function
doing the same in firefox gives me
TypeError: function () { } is not a constructor
This does not help to define the class constructor, as shown in the current MDN function example; if I try to instantiate this class:
class bar extends null { constructor(){} }
then Chrome / Node tell me:
ReferenceError: this is not defined
and Firefox tells me:
ReferenceError: |this| used uninitialized in bar class constructor
What is this madness? Why are these nullable classes incompatible? And considering that they are not real, why did the opportunity to create them appear deliberately gone into the specification, and why did some MDN author think it was remarkable enough for documentation ? What is the possible use case for this function?
javascript ecmascript-6 es6-class
Mark Amery Dec 16 '16 at 17:06 2016-12-16 17:06
source share