How and why would I write a class that extends null?

后端 未结 2 1380
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 05:20

JavaScript\'s class syntax, added in ES6, apparently makes it legal to extend null:

class foo extends null {}

Some Googling reveals that it was

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 05:59

    Instantiating such classes is meant to work; Chrome and Firefox just have bugs. Here's Chrome's, here's Firefox's. It works fine in Safari (at least on master).

    There used to be a bug in the spec which made them impossible to instantiate, but it's been fixed for a while. (There's still a related one, but that's not what you're seeing.)

    The use case is roughly the same as that of Object.create(null). Sometimes you want something which doesn't inherit from Object.prototype.

提交回复
热议问题