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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Built-in classes likeArray, Mapand others are extendable also.
Las clases integradas comoArray, Mapy otras también son extensibles.
For instance, here `PowerArray`inherits from the native `Array`:
Por ejemplo, aquí `PowerArray`hereda del nativo `Array`:
```js run
//add one more method to it (can do more)
//se agrega un método más (puedes hacer más)
class PowerArray extends Array {
isEmpty() {
return this.length === 0;
}
}
let arr = new PowerArray(1, 2, 5, 10, 50);
alert(arr.isEmpty()); //false
alert(arr.isEmpty()); //falso
let filteredArr = arr.filter(item => item >= 10);
alert(filteredArr); // 10, 50
alert(filteredArr.isEmpty()); //false
alert(filteredArr.isEmpty()); //falso
```
Please note a very interesting thing. Built-in methods like `filter`, `map`and others -- return new objects of exactly the inherited type`PowerArray`.Their internal implementation uses the object's `constructor`property for that.
Tenga en cuenta una cosa muy interesante. Métodos incorporados como `filter`, `map`y otros: devuelven nuevos objetos exactamente del tipo heredado`PowerArray`.Su implementación interna utiliza la propiedad `constructor`del objeto para eso.
In the example above,
En el ejemplo anterior,
```js
arr.constructor === PowerArray
```
When`arr.filter()` is called, it internally creates the new array of results using exactly`arr.constructor`,not basic`Array`.That's actually very cool, because we can keep using `PowerArray`methods further on the result.
Cuando se llama a`arr.filter()`, crea internamente la nueva matriz de resultados usando exactamente`arr.constructor`,no el básico`Array`.En realidad, eso es muy bueno, porque podemos seguir usando métodos `PowerArray`más adelante en el resultado.
Even more, we can customize that behavior.
Aún más, podemos personalizar ese comportamiento.
We can add a special static getter`Symbol.species`to the class. If it exists, it should return theconstructorthat JavaScriptwill use internally to create new entities in`map`, `filter`and so on.
Podemos agregar un `getter` estático especial`Symbol.species`a la clase. Si existe, debería devolver elconstructorque JavaScriptusará internamente para crear nuevas entidades en`map`, `filter`y así sucesivamente.
If we'd like built-in methods like`map`or `filter`to return regular arrays, we can return`Array`in `Symbol.species`,like here:
Si queremos que los métodos incorporados como`map`o `filter`devuelvan matrices regulares, podemos devolver`Array`en `Symbol.species`,como aquí:
```js run
class PowerArray extends Array {
Expand All
@@ -43,47 +43,47 @@ class PowerArray extends Array {
}
*!*
//built-in methods will use this as the constructor
//los métodos incorporados usarán esto como el constructor
static get [Symbol.species]() {
return Array;
}
*/!*
}
let arr = new PowerArray(1, 2, 5, 10, 50);
alert(arr.isEmpty()); //false
alert(arr.isEmpty()); //falso
// filtercreates new array usingarr.constructor[Symbol.species]as constructor
// filtercrea una nueva matriz usandoarr.constructor[Symbol.species]como constructor
let filteredArr = arr.filter(item => item >= 10);
*!*
//filteredArr is not PowerArray,but Array
//filterArr no es PowerArray,sino Array
*/!*
alert(filteredArr.isEmpty()); // Error:filteredArr.isEmptyis not a function
alert(filteredArr.isEmpty()); // Error:filterArr.isEmptyno es una función
```
As you can see, now `.filter`returns `Array`.So the extended functionality is not passed any further.
Como puede ver, ahora `.filter`devuelve `Array`.Por lo tanto, la funcionalidad extendida ya no se pasa.
```smart header="Other collectionswork similarly"
Other collections, such as`Map`and `Set`,work alike. They also use `Symbol.species`.
Otras colecciones, como`Map`y `Set`,funcionan igual. También usan `Symbol.species`.
```
##No static inheritance in built-ins
##Sin herencia estática en incorporados
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
Los objetos incorporados tienen sus propios métodos estáticos, por ejemplo, `Object.keys`, `Array.isArray`, etc.
As we already know, native classes extend each other. For instance, `Array`extends `Object`.
Como ya sabemos, las clases nativas se extienden entre sí. Por ejemplo, `Array`extiende `Object`.
Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the article [](info:static-properties-methods#statics-and-inheritance).
Normalmente, cuando una clase extiende a otra, se heredan los métodos estáticos y no estáticos. Eso se explicó a fondo en el artículo [](info:static-properties-methods#statics-and-inheritance).
But built-in classes are an exception. They don't inherit statics from each other.
Pero las clases integradas son una excepción. No heredan estáticos el uno del otro.
For example, both `Array`and `Date`inherit from `Object`,so their instances have methods from`Object.prototype`.But `Array.[[Prototype]]`does not reference`Object`,so there's no, for instance,`Array.keys()` (or `Date.keys()`) static method.
Por ejemplo, tanto `Array`como `Date`heredan de `Object`,por lo que sus instancias tienen métodos de`Object.prototype`.Pero `Array.[[Prototype]]`no hace referencia a`Object`,por lo que no existe, por ejemplo, el método estático`Array.keys()` (o `Date.keys()`).
Here's the picture structure for`Date`and `Object`:
Aquí está la imagen, estructura para`Date`y `Object`:

As you can see, there'snolink between`Date`and `Object`.They are independent, only `Date.prototype`inherits from `Object.prototype`.
Como puede ver,nohay un vínculo entre`Date`y `Object`.Son independientes, solo `Date.prototype`hereda de `Object.prototype`.
That's an important difference of inheritance between built-in objects compared to what we get with `extends`.
Esa es una diferencia importante de herencia entre los objetos integrados en comparación con lo que obtenemos con 'extends`.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.