Skip to content

Commit c693192

Browse files
committed
improvement
1 parent 634db37 commit c693192

File tree

2 files changed

+13
-19
lines changed
  • 1-js/07-object-oriented-programming/10-class-inheritance/3-class-extend-object

2 files changed

+13
-19
lines changed

1-js/07-object-oriented-programming/10-class-inheritance/3-class-extend-object/solution.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The answer has two parts.
1+
First, let's see why the latter code doesn't work.
22

3-
The first, an easy one is that the inheriting class needs to call `super()` in the constructor. Otherwise `"this"` won't be "defined".
3+
The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined".
44

55
So here's the fix:
66

@@ -37,7 +37,7 @@ alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
3737
alert( Rabbit.__proto__ === Object ); // (2) true
3838
```
3939

40-
So we can access static methods of `Object` via `Rabbit`, like this:
40+
So `Rabbit` now provides access to static methods of `Object` via `Rabbit`, like this:
4141

4242
```js run
4343
class Rabbit extends Object {}
@@ -48,30 +48,24 @@ alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // a,b
4848
*/!*
4949
```
5050

51-
And if we don't use `extends`, then `class Rabbit` does not get the second reference.
51+
But if we don't have `extends Object`, then `Rabbit.__proto__` is not set to `Object`.
5252

53-
Please compare with it:
53+
Here's the demo:
5454

5555
```js run
5656
class Rabbit {}
5757

5858
alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
5959
alert( Rabbit.__proto__ === Object ); // (2) false (!)
60+
alert( Rabbit.__proto__ === Function.prototype ); // as any function by default
6061

6162
*!*
6263
// error, no such function in Rabbit
6364
alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // Error
6465
*/!*
6566
```
6667

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-
```
68+
So `Rabbit` doesn't provide access to static methods of `Object` in that case.
7569

7670
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`.
7771

1-js/07-object-oriented-programming/10-class-inheritance/3-class-extend-object/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ importance: 5
44

55
# Class extends Object?
66

7-
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods.
7+
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods like `hasOwnProperty` etc.
88

9-
Like demonstrated here:
9+
For instance:
1010

1111
```js run
1212
class Rabbit {
@@ -24,9 +24,11 @@ alert( rabbit.hasOwnProperty('name') ); // true
2424
*/!*
2525
```
2626

27-
So, is it correct to say that `"class Rabbit extends Object"` does exactly the same as `"class Rabbit"`, or not?
27+
But if we spell it out explicitly like `"class Rabbit extends Object"`, then the result would be different from a simple `"class Rabbit"`?
2828

29-
Will it work?
29+
What's the difference?
30+
31+
Here's an example of such code (it doesn't work -- why? fix it?):
3032

3133
```js
3234
class Rabbit extends Object {
@@ -39,5 +41,3 @@ let rabbit = new Rabbit("Rab");
3941

4042
alert( rabbit.hasOwnProperty('name') ); // true
4143
```
42-
43-
If it won't please fix the code.

0 commit comments

Comments
 (0)