Skip to content

Commit c348fff

Browse files
authored
Merge pull request #174 from cortizg/es.javascript.info.1-09-05
Extending built-in classes 1-9-5 arreglar linea 62
2 parents 82e9523 + 87eaae5 commit c348fff

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11

2-
# Extending built-in classes
2+
# Ampliación de clases integradas
33

4-
Built-in classes like Array, Map and others are extendable also.
4+
Las clases integradas como Array, Map y otras también son extensibles.
55

6-
For instance, here `PowerArray` inherits from the native `Array`:
6+
Por ejemplo, aquí `PowerArray` hereda del nativo `Array`:
77

88
```js run
9-
// add one more method to it (can do more)
9+
// se agrega un método más (puedes hacer más)
1010
class PowerArray extends Array {
1111
isEmpty() {
1212
return this.length === 0;
1313
}
1414
}
1515

1616
let arr = new PowerArray(1, 2, 5, 10, 50);
17-
alert(arr.isEmpty()); // false
17+
alert(arr.isEmpty()); // falso
1818

1919
let filteredArr = arr.filter(item => item >= 10);
2020
alert(filteredArr); // 10, 50
21-
alert(filteredArr.isEmpty()); // false
21+
alert(filteredArr.isEmpty()); // falso
2222
```
2323

24-
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.
24+
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.
2525

26-
In the example above,
26+
En el ejemplo anterior,
2727
```js
2828
arr.constructor === PowerArray
2929
```
3030

31-
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.
31+
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.
3232

33-
Even more, we can customize that behavior.
33+
Aún más, podemos personalizar ese comportamiento.
3434

35-
We can add a special static getter `Symbol.species` to the class. If it exists, it should return the constructor that JavaScript will use internally to create new entities in `map`, `filter` and so on.
35+
Podemos agregar un `getter` estático especial `Symbol.species` a la clase. Si existe, debería devolver el constructor que JavaScript usará internamente para crear nuevas entidades en `map`, `filter` y así sucesivamente.
3636

37-
If we'd like built-in methods like `map` or `filter` to return regular arrays, we can return `Array` in `Symbol.species`, like here:
37+
Si queremos que los métodos incorporados como `map` o `filter` devuelvan matrices regulares, podemos devolver `Array` en `Symbol.species`, como aquí:
3838

3939
```js run
4040
class PowerArray extends Array {
@@ -43,47 +43,47 @@ class PowerArray extends Array {
4343
}
4444

4545
*!*
46-
// built-in methods will use this as the constructor
46+
// los métodos incorporados usarán esto como el constructor
4747
static get [Symbol.species]() {
4848
return Array;
4949
}
5050
*/!*
5151
}
5252

5353
let arr = new PowerArray(1, 2, 5, 10, 50);
54-
alert(arr.isEmpty()); // false
54+
alert(arr.isEmpty()); // falso
5555

56-
// filter creates new array using arr.constructor[Symbol.species] as constructor
56+
// filter crea una nueva matriz usando arr.constructor[Symbol.species] como constructor
5757
let filteredArr = arr.filter(item => item >= 10);
5858

5959
*!*
60-
// filteredArr is not PowerArray, but Array
60+
// filterArr no es PowerArray, sino Array
6161
*/!*
62-
alert(filteredArr.isEmpty()); // Error: filteredArr.isEmpty is not a function
62+
alert(filteredArr.isEmpty()); // Error: filterArr.isEmpty no es una función
6363
```
6464

65-
As you can see, now `.filter` returns `Array`. So the extended functionality is not passed any further.
65+
Como puede ver, ahora `.filter` devuelve `Array`. Por lo tanto, la funcionalidad extendida ya no se pasa.
6666

67-
```smart header="Other collections work similarly"
68-
Other collections, such as `Map` and `Set`, work alike. They also use `Symbol.species`.
67+
```smart header="Other collections trabaja similarmente"
68+
Otras colecciones, como `Map` y `Set`, funcionan igual. También usan `Symbol.species`.
6969
```
7070

71-
## No static inheritance in built-ins
71+
## Sin herencia estática en incorporados
7272

73-
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
73+
Los objetos incorporados tienen sus propios métodos estáticos, por ejemplo, `Object.keys`, `Array.isArray`, etc.
7474

75-
As we already know, native classes extend each other. For instance, `Array` extends `Object`.
75+
Como ya sabemos, las clases nativas se extienden entre sí. Por ejemplo, `Array` extiende `Object`.
7676

77-
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).
77+
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).
7878

79-
But built-in classes are an exception. They don't inherit statics from each other.
79+
Pero las clases integradas son una excepción. No heredan estáticos el uno del otro.
8080

81-
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.
81+
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()`).
8282

83-
Here's the picture structure for `Date` and `Object`:
83+
Aquí está la imagen, estructura para `Date` y `Object`:
8484

8585
![](object-date-inheritance.svg)
8686

87-
As you can see, there's no link between `Date` and `Object`. They are independent, only `Date.prototype` inherits from `Object.prototype`.
87+
Como puede ver, no hay un vínculo entre `Date` y `Object`. Son independientes, solo `Date.prototype` hereda de `Object.prototype`.
8888

89-
That's an important difference of inheritance between built-in objects compared to what we get with `extends`.
89+
Esa es una diferencia importante de herencia entre los objetos integrados en comparación con lo que obtenemos con 'extends`.

0 commit comments

Comments
 (0)