|
| 1 | +The answer has two parts. |
| 2 | + |
| 3 | +The first, an easy one is that the inheriting class needs to call `super()` in the constructor. Otherwise `"this"` won't be "defined". |
| 4 | + |
| 5 | +So here's the fix: |
| 6 | + |
| 7 | +```js run |
| 8 | +class Rabbit extends Object { |
| 9 | + constructor(name) { |
| 10 | +*!* |
| 11 | + super(); // need to call the parent constructor when inheriting |
| 12 | +*/!* |
| 13 | + this.name = name; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +let rabbit = new Rabbit("Rab"); |
| 18 | + |
| 19 | +alert( rabbit.hasOwnProperty('name') ); // true |
| 20 | +``` |
| 21 | + |
| 22 | +But that's not all yet. |
| 23 | + |
| 24 | +Even after the fix, there's still important difference in `"class Rabbit extends Object"` versus `class Rabbit`. |
| 25 | + |
| 26 | +As we know, the "extends" syntax sets up two prototypes: |
| 27 | + |
| 28 | +1. Between `"prototype"` of the constructor functions (for methods). |
| 29 | +2. Between the constructor functions itself (for static methods). |
| 30 | + |
| 31 | +In our case, for `class Rabbit extends Object` it means: |
| 32 | + |
| 33 | +```js run |
| 34 | +class Rabbit extends Object {} |
| 35 | + |
| 36 | +alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true |
| 37 | +alert( Rabbit.__proto__ === Object ); // (2) true |
| 38 | +``` |
| 39 | + |
| 40 | +So we can access static methods of `Object` via `Rabbit`, like this: |
| 41 | + |
| 42 | +```js run |
| 43 | +class Rabbit extends Object {} |
| 44 | + |
| 45 | +*!* |
| 46 | +// normally we call Object.getOwnPropertyNames |
| 47 | +alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // a,b |
| 48 | +*/!* |
| 49 | +``` |
| 50 | + |
| 51 | +And if we don't use `extends`, then `class Rabbit` does not get the second reference. |
| 52 | + |
| 53 | +Please compare with it: |
| 54 | + |
| 55 | +```js run |
| 56 | +class Rabbit {} |
| 57 | + |
| 58 | +alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true |
| 59 | +alert( Rabbit.__proto__ === Object ); // (2) false (!) |
| 60 | + |
| 61 | +*!* |
| 62 | +// error, no such function in Rabbit |
| 63 | +alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // Error |
| 64 | +*/!* |
| 65 | +``` |
| 66 | + |
| 67 | +For the simple `class Rabbit`, the `Rabbit` function has the same prototype |
| 68 | + |
| 69 | +```js run |
| 70 | +class Rabbit {} |
| 71 | + |
| 72 | +// instead of (2) that's correct for Rabbit (just like any function): |
| 73 | +alert( Rabbit.__proto__ === Function.prototype ); |
| 74 | +``` |
| 75 | + |
| 76 | +By the way, `Function.prototype` has "generic" function methods, like `call`, `bind` etc. They are ultimately available in both cases, because for the built-in `Object` constructor, `Object.__proto__ === Function.prototype`. |
| 77 | + |
| 78 | +Here's the picture: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +So, to put it short, there are two differences: |
| 83 | + |
| 84 | +| class Rabbit | class Rabbit extends Object | |
| 85 | +|--------------|------------------------------| |
| 86 | +| -- | needs to call `super()` in constructor | |
| 87 | +| `Rabbit.__proto__ === Function.prototype` | `Rabbit.__proto__ === Object` | |
0 commit comments