|
1 |
| -First, let's see why the latter code doesn't work. |
| 1 | +Primero, veamos por qué el código anterior no funciona. |
2 | 2 |
|
3 |
| -The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined". |
| 3 | +La razón se vuelve evidente si intentamos ejecutarlo. Un constructor de clase heredado tiene que llamar a `super()`. De lo contrario `"this"` no será "definido". |
4 | 4 |
|
5 |
| -So here's the fix: |
| 5 | +Así que aquí está la solución: |
6 | 6 |
|
7 | 7 | ```js run
|
8 | 8 | class Rabbit extends Object {
|
9 | 9 | constructor(name) {
|
10 | 10 | *!*
|
11 |
| - super(); // need to call the parent constructor when inheriting |
| 11 | + super(); // necesita llamar al constructor padre cuando se hereda |
12 | 12 | */!*
|
13 | 13 | this.name = name;
|
14 | 14 | }
|
15 | 15 | }
|
16 | 16 |
|
17 | 17 | let rabbit = new Rabbit("Rab");
|
18 | 18 |
|
19 |
| -alert( rabbit.hasOwnProperty('name') ); // true |
| 19 | +alert( rabbit.hasOwnProperty('name') ); // verdadero |
20 | 20 | ```
|
21 | 21 |
|
22 |
| -But that's not all yet. |
| 22 | +Pero eso no es todo. |
23 | 23 |
|
24 |
| -Even after the fix, there's still important difference in `"class Rabbit extends Object"` versus `class Rabbit`. |
| 24 | +Incluso después de arreglarlo, aún existe una diferencia importante en `"class Rabbit extends Object"` versus `class Rabbit`. |
25 | 25 |
|
26 |
| -As we know, the "extends" syntax sets up two prototypes: |
| 26 | +Como sabemos, la sintaxis "extends" configura dos prototipos: |
27 | 27 |
|
28 |
| -1. Between `"prototype"` of the constructor functions (for methods). |
29 |
| -2. Between the constructor functions themselves (for static methods). |
| 28 | +1. Entre `"prototype"` de las funciones del constructor (para métodos). |
| 29 | +2. Entre las propias funciones del constructor (para métodos estáticos). |
30 | 30 |
|
31 |
| -In our case, for `class Rabbit extends Object` it means: |
| 31 | +En nuestro caso, para `class Rabbit extends Object` significa: |
32 | 32 |
|
33 | 33 | ```js run
|
34 | 34 | class Rabbit extends Object {}
|
35 | 35 |
|
36 |
| -alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true |
37 |
| -alert( Rabbit.__proto__ === Object ); // (2) true |
| 36 | +alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) verdadero |
| 37 | +alert( Rabbit.__proto__ === Object ); // (2) verdadero |
38 | 38 | ```
|
39 | 39 |
|
40 |
| -So `Rabbit` now provides access to static methods of `Object` via `Rabbit`, like this: |
| 40 | +Entonces `Rabbit` ahora proporciona acceso a los métodos estáticos de `Object` a través de `Rabbit`, así: |
41 | 41 |
|
42 | 42 | ```js run
|
43 | 43 | class Rabbit extends Object {}
|
44 | 44 |
|
45 | 45 | *!*
|
46 |
| -// normally we call Object.getOwnPropertyNames |
| 46 | +// normalmente llamamos a Object.getOwnPropertyNames |
47 | 47 | alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // a,b
|
48 | 48 | */!*
|
49 | 49 | ```
|
50 | 50 |
|
51 |
| -But if we don't have `extends Object`, then `Rabbit.__proto__` is not set to `Object`. |
| 51 | +Pero si no tenemos `extends Object`, entonces `Rabbit.__proto__` no está definido como `Object`. |
52 | 52 |
|
53 |
| -Here's the demo: |
| 53 | +Aquí está la demostración: |
54 | 54 |
|
55 | 55 | ```js run
|
56 | 56 | class Rabbit {}
|
57 | 57 |
|
58 |
| -alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true |
59 |
| -alert( Rabbit.__proto__ === Object ); // (2) false (!) |
60 |
| -alert( Rabbit.__proto__ === Function.prototype ); // as any function by default |
| 58 | +alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) verdadero |
| 59 | +alert( Rabbit.__proto__ === Object ); // (2) falso (!) |
| 60 | +alert( Rabbit.__proto__ === Function.prototype ); // como cualquier función por defecto |
61 | 61 |
|
62 | 62 | *!*
|
63 |
| -// error, no such function in Rabbit |
| 63 | +// error, no existe esta función en Rabbit |
64 | 64 | alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // Error
|
65 | 65 | */!*
|
66 | 66 | ```
|
67 | 67 |
|
68 |
| -So `Rabbit` doesn't provide access to static methods of `Object` in that case. |
| 68 | +Entonces `Rabbit` no proporciona acceso a métodos estáticos de `Object` en este caso. |
69 | 69 |
|
70 |
| -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`. |
| 70 | +Por cierto, `Function.prototype` tiene métodos de función "genéricos", como `call`, `bind` etc. Finalmente, están disponibles en ambos casos, por el `Object` que tiene el constructor incorporado `Object.__proto__ === Function.prototype`. |
71 | 71 |
|
72 |
| -Here's the picture: |
| 72 | +Aquí está la imagen: |
73 | 73 |
|
74 | 74 | 
|
75 | 75 |
|
76 |
| -So, to put it short, there are two differences: |
| 76 | +Por lo tanto, en pocas palabras, existen dos diferencias: |
77 | 77 |
|
78 | 78 | | class Rabbit | class Rabbit extends Object |
|
79 | 79 | |--------------|------------------------------|
|
80 |
| -| -- | needs to call `super()` in constructor | |
| 80 | +| -- | necesita llamar a `super()` en el constructor | |
81 | 81 | | `Rabbit.__proto__ === Function.prototype` | `Rabbit.__proto__ === Object` |
|
0 commit comments