improvement
This commit is contained in:
parent
634db3741c
commit
c693192018
2 changed files with 13 additions and 19 deletions
|
@ -1,6 +1,6 @@
|
|||
The answer has two parts.
|
||||
First, let's see why the latter code doesn't work.
|
||||
|
||||
The first, an easy one is that the inheriting class needs to call `super()` in the constructor. Otherwise `"this"` won't be "defined".
|
||||
The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined".
|
||||
|
||||
So here's the fix:
|
||||
|
||||
|
@ -37,7 +37,7 @@ alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
|
|||
alert( Rabbit.__proto__ === Object ); // (2) true
|
||||
```
|
||||
|
||||
So we can access static methods of `Object` via `Rabbit`, like this:
|
||||
So `Rabbit` now provides access to static methods of `Object` via `Rabbit`, like this:
|
||||
|
||||
```js run
|
||||
class Rabbit extends Object {}
|
||||
|
@ -48,15 +48,16 @@ alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // a,b
|
|||
*/!*
|
||||
```
|
||||
|
||||
And if we don't use `extends`, then `class Rabbit` does not get the second reference.
|
||||
But if we don't have `extends Object`, then `Rabbit.__proto__` is not set to `Object`.
|
||||
|
||||
Please compare with it:
|
||||
Here's the demo:
|
||||
|
||||
```js run
|
||||
class Rabbit {}
|
||||
|
||||
alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
|
||||
alert( Rabbit.__proto__ === Object ); // (2) false (!)
|
||||
alert( Rabbit.__proto__ === Function.prototype ); // as any function by default
|
||||
|
||||
*!*
|
||||
// error, no such function in Rabbit
|
||||
|
@ -64,14 +65,7 @@ alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // Error
|
|||
*/!*
|
||||
```
|
||||
|
||||
For the simple `class Rabbit`, the `Rabbit` function has the same prototype
|
||||
|
||||
```js run
|
||||
class Rabbit {}
|
||||
|
||||
// instead of (2) that's correct for Rabbit (just like any function):
|
||||
alert( Rabbit.__proto__ === Function.prototype );
|
||||
```
|
||||
So `Rabbit` doesn't provide access to static methods of `Object` in that case.
|
||||
|
||||
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`.
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ importance: 5
|
|||
|
||||
# Class extends Object?
|
||||
|
||||
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods.
|
||||
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods like `hasOwnProperty` etc.
|
||||
|
||||
Like demonstrated here:
|
||||
For instance:
|
||||
|
||||
```js run
|
||||
class Rabbit {
|
||||
|
@ -24,9 +24,11 @@ alert( rabbit.hasOwnProperty('name') ); // true
|
|||
*/!*
|
||||
```
|
||||
|
||||
So, is it correct to say that `"class Rabbit extends Object"` does exactly the same as `"class Rabbit"`, or not?
|
||||
But if we spell it out explicitly like `"class Rabbit extends Object"`, then the result would be different from a simple `"class Rabbit"`?
|
||||
|
||||
Will it work?
|
||||
What's the difference?
|
||||
|
||||
Here's an example of such code (it doesn't work -- why? fix it?):
|
||||
|
||||
```js
|
||||
class Rabbit extends Object {
|
||||
|
@ -39,5 +41,3 @@ let rabbit = new Rabbit("Rab");
|
|||
|
||||
alert( rabbit.hasOwnProperty('name') ); // true
|
||||
```
|
||||
|
||||
If it won't please fix the code.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue