You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/09-classes/05-extend-natives/article.md
+14-6
Original file line number
Diff line number
Diff line change
@@ -28,12 +28,11 @@ In the example above,
28
28
arr.constructor=== PowerArray
29
29
```
30
30
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.
33
32
34
33
Even more, we can customize that behavior.
35
34
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.
37
36
38
37
If we'd like built-in methods like `map`, `filter` will return regular arrays, we can return `Array` in `Symbol.species`, like here:
39
38
@@ -69,11 +68,20 @@ As you can see, now `.filter` returns `Array`. So the extended functionality is
69
68
70
69
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
71
70
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`.
73
72
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.
75
74
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()`.
77
85
78
86
Here's the picture structure for `Date` and `Object`:
0 commit comments