Skip to content

Commit e1320b2

Browse files
committed
Merge branch 'master' of github.com:denysdovhan/wtfjs
2 parents a6f6d37 + 00c3985 commit e1320b2

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,65 @@ new Foo() instanceof null;
497497

498498
This is not a part of the specification. It's just a bug that has now been fixed, so there shouldn't be a problem with it in the future.
499499

500+
### Super constructor null of Foo is not a constructor
501+
502+
It's continuation of story with previous bug in modern environment (tested with Chrome 71 and Node.js v11.8.0).
503+
504+
```js
505+
class Foo extends null {}
506+
new Foo() instanceof null;
507+
// > TypeError: Super constructor null of Foo is not a constructor
508+
```
509+
510+
### 💡 Explanation:
511+
512+
This is not a bug because:
513+
514+
```js
515+
Object.getPrototypeOf(Foo.prototype); // -> null
516+
```
517+
518+
If the class has no constructor the call from prototype chain. But in the parent has no constructor. Just in case, I’ll clarify that `null` is an object:
519+
520+
```js
521+
typeof null === 'object'
522+
```
523+
524+
Therefore, you can inherit from it (although in the world of the OOP for such terms would have beaten me). So you can't call the null constructor. If you change this code:
525+
526+
```js
527+
class Foo extends null {
528+
constructor() {
529+
console.log('something');
530+
}
531+
}
532+
```
533+
534+
You see the error:
535+
536+
```
537+
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
538+
```
539+
540+
And if you add `super`:
541+
542+
```js
543+
class Foo extends null {
544+
constructor() {
545+
console.log(111);
546+
super();
547+
}
548+
}
549+
```
550+
551+
JS throws an error:
552+
553+
```
554+
TypeError: Super constructor null of Foo is not a constructor
555+
```
556+
557+
- [An explanation of this issue](https://github.com/denysdovhan/wtfjs/pull/102#discussion_r259143582) by [@geekjob](https://github.com/geekjob)
558+
500559
## Adding arrays
501560

502561
What if you try to add two arrays?

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wtfjs",
3-
"version": "1.18.0",
3+
"version": "1.19.0",
44
"description": "A list of funny and tricky JavaScript examples",
55
"bin": {
66
"wtfjs": "wtfjs.js"

0 commit comments

Comments
 (0)