Skip to content

Commit cfff5c1

Browse files
committed
minor
1 parent 7d6d436 commit cfff5c1

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

1-js/09-classes/05-extend-natives/article.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ In the example above,
2828
arr.constructor === PowerArray
2929
```
3030

31-
So when `arr.filter()` is called, it internally creates the new array of results exactly as `new PowerArray`.
32-
That's actually very cool, because we can keep using `PowerArray` methods further on the result.
31+
So when `arr.filter()` is called, it internally creates the new array of results using exactly `new PowerArray`, not basic `Array`. That's actually very cool, because we can keep using `PowerArray` methods further on the result.
3332

3433
Even more, we can customize that behavior.
3534

36-
There's a special static getter `Symbol.species`, if exists, it returns the constructor to use in such cases.
35+
We can add a special static getter `Symbol.species` to the class. If exists, it should return the constructor that JavaScript will use internally to create new entities in `map`, `filter` and so on.
3736

3837
If we'd like built-in methods like `map`, `filter` will return regular arrays, we can return `Array` in `Symbol.species`, like here:
3938

@@ -69,11 +68,20 @@ As you can see, now `.filter` returns `Array`. So the extended functionality is
6968

7069
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
7170

72-
And we've already been talking about native classes extending each other: `Array.[[Prototype]] = Object`.
71+
As we already know, native classes extend each other. For instance, `Array` extends `Object`.
7372

74-
But statics are an exception. Built-in classes don't inherit static properties from each other.
73+
Normally, when one class extends another, both static and non-static methods are inherited.
7574

76-
In other words, the prototype of built-in constructor `Array` does not point to `Object`. This way `Array` and `Date` do not have `Array.keys` or `Date.keys`. And that feels natural.
75+
So, if `Rabbit extends Animal`, then:
76+
77+
1. `Rabbit.methods` are callable for `Animal.methods`, because `Rabbit.[[Prototype]] = Animal`.
78+
2. `new Rabbit().methods` are also available, because `Rabbit.prototype.[[Prototype]] = Animal.prototype`.
79+
80+
That's thoroughly explained in the chapter [](info:static-properties-methods#statics-and-inheritance).
81+
82+
But built-in classes are an exception. They don't inherit statics `(1)` from each other.
83+
84+
For example, both `Array` and `Date` inherit from `Object`, so their instances have methods from `Object.prototype`. But `Array.[[Prototype]]` does not point to `Object`. So there's `Object.keys()`, but not `Array.keys()` and `Date.keys()`.
7785

7886
Here's the picture structure for `Date` and `Object`:
7987

0 commit comments

Comments
 (0)