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
The value of`this`in `User.staticMethod()`call is the class constructor `User`itself (the "object before dot" rule).
32
+
El valor de`this`en la llamada `User.staticMethod()`es el mismo constructor de clase `User`(la regla "objeto antes de punto").
33
33
34
-
Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.
34
+
Por lo general, los métodos estáticos se utilizan para implementar funciones que pertenecen a la clase, pero no a ningún objeto particular de la misma.
35
35
36
-
For instance, we have`Article`objects and need a function to compare them. A natural solution would be to add `Article.compare` method, like this:
36
+
Por ejemplo, tenemos objetos`Article`y necesitamos una función para compararlos. Una solución natural sería agregar el método `Article.compare`, como este:
Here`Article.compare`stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
66
+
Aquí`Article.compare`se encuentra "encima" de los artículos, como un medio para compararlos. No es el método de un artículo, sino de toda la clase.
67
67
68
-
Another example would be a so-called "factory" method. Imagine, we need few ways to create an article:
68
+
Otro ejemplo sería un método llamado "factory". Imagina, necesitamos pocas formas para crear un artículo:
69
69
70
-
1.Create by given parameters (`title`,`date` etc).
71
-
2.Create an empty article with today's date.
72
-
3. ...or else somehow.
70
+
1.Crearlo por parámetros dados (`title`,`date` etc.).
71
+
2.Crear un artículo vacío con la fecha de hoy.
72
+
3. ... o cualquier otra manera.
73
73
74
-
The first way can be implemented by the constructor. And for the second one we can make a static method of the class.
74
+
La primera forma puede ser implementada por el constructor. Y para el segundo podemos hacer un método estático de la clase.
75
75
76
-
Like `Article.createTodays()`here:
76
+
Al igual que `Article.createTodays()`aquí:
77
77
78
78
```js run
79
79
classArticle {
@@ -84,32 +84,31 @@ class Article {
84
84
85
85
*!*
86
86
staticcreateTodays() {
87
-
//remember, this = Article
88
-
returnnewthis("Today's digest", newDate());
87
+
//recuerda, this = Article
88
+
returnnewthis("Resumen de hoy", newDate());
89
89
}
90
90
*/!*
91
91
}
92
92
93
93
let article =Article.createTodays();
94
94
95
-
alert( article.title ); //Today's digest
95
+
alert( article.title ); //Resumen de hoy
96
96
```
97
+
Ahora, cada vez que necesitamos crear un resumen de hoy, podemos llamar a `Article.createTodays()`. Una vez más, ese no es el método de un objeto artículo, sino el método de toda la clase.
97
98
98
-
Now every time we need to create a today's digest, we can call `Article.createTodays()`. Once again, that's not a method of an article, but a method of the whole class.
99
-
100
-
Static methods are also used in database-related classes to search/save/remove entries from the database, like this:
99
+
Los métodos estáticos también se utilizan en clases relacionadas con base de datos para buscar/guardar/eliminar entradas de la misma, como esta:
101
100
102
101
```js
103
-
//assuming Article is a special class for managing articles
104
-
//static method to remove the article:
102
+
//suponiendo que el artículo es una clase especial para gestionar artículos
103
+
//método estático para eliminar el artículo:
105
104
Article.remove({id:12345});
106
105
```
107
106
108
-
## Static properties
107
+
## Propiedades estáticas
109
108
110
109
[recent browser=Chrome]
111
110
112
-
Static properties are also possible, they look like regular class properties, but prepended by`static`:
111
+
Las propiedades estáticas también son posibles, se ven como propiedades de clase regular, pero precedidas por`static`:
113
112
114
113
```js run
115
114
classArticle {
@@ -119,30 +118,29 @@ class Article {
119
118
alert( Article.publisher ); // Ilya Kantor
120
119
```
121
120
122
-
That is the same as a direct assignment to`Article`:
121
+
Eso es lo mismo que una asignación directa a`Article`:
123
122
124
123
```js
125
124
Article.publisher="Ilya Kantor";
126
125
```
127
126
128
-
## Inheritance of static properties and methods
127
+
## Herencia de propiedades y métodos estáticos.
129
128
130
-
Static properties and methods are inherited.
129
+
Las propiedades y métodos estáticos son heredados.
131
130
132
-
For instance, `Animal.compare`and`Animal.planet`in the code below are inherited and accessible as`Rabbit.compare`and`Rabbit.planet`:
131
+
Por ejemplo, `Animal.compare`y`Animal.planet`en el siguiente código son heredados y accesibles como`Rabbit.compare`y`Rabbit.planet`:
133
132
134
133
```js run
135
134
classAnimal {
136
-
static planet ="Earth";
137
-
135
+
static planet ="Tierra";
138
136
constructor(name, speed) {
139
137
this.speed= speed;
140
138
this.name= name;
141
139
}
142
140
143
141
run(speed=0) {
144
142
this.speed+= speed;
145
-
alert(`${this.name}runs with speed${this.speed}.`);
143
+
alert(`${this.name}corre a una velocidad de${this.speed}.`);
146
144
}
147
145
148
146
*!*
@@ -153,64 +151,64 @@ class Animal {
153
151
154
152
}
155
153
156
-
//Inherit from Animal
154
+
//Hereda de Animal
157
155
classRabbitextendsAnimal {
158
156
hide() {
159
-
alert(`${this.name}hides!`);
157
+
alert(`${this.name}se esconde!`);
160
158
}
161
159
}
162
160
163
161
let rabbits = [
164
-
newRabbit("White Rabbit", 10),
165
-
newRabbit("Black Rabbit", 5)
162
+
newRabbit("Conejo Blanco", 10),
163
+
newRabbit("Conejo Negro", 5)
166
164
];
167
165
168
166
*!*
169
167
rabbits.sort(Rabbit.compare);
170
168
*/!*
171
169
172
-
rabbits[0].run(); //Black Rabbit runs with speed 5.
170
+
rabbits[0].run(); //Conejo Negro corre a una velocidad de 5.
173
171
174
-
alert(Rabbit.planet); //Earth
172
+
alert(Rabbit.planet); //Tierra
175
173
```
176
174
177
-
Now when we call`Rabbit.compare`, the inherited `Animal.compare`will be called.
175
+
Ahora, cuando llamemos a`Rabbit.compare`, se llamará a `Animal.compare`heredado.
178
176
179
-
How does it work? Again, using prototypes. As you might have already guessed, `extends`gives `Rabbit`the`[[Prototype]]`reference to`Animal`.
177
+
¿Como funciona? Nuevamente, usando prototipos. Como ya habrás adivinado, `extends`da a `Rabbit`el`[[Prototype]]`referente a`Animal`.
180
178
181
179

182
180
183
-
So, `Rabbit extends Animal`creates two `[[Prototype]]` references:
181
+
Entonces, `Rabbit extends Animal`crea dos referencias `[[Prototype]]`:
184
182
185
-
1.`Rabbit`function prototypally inherits from `Animal` function.
Static methods are used for the functionality that belongs to the class "as a whole". It doesn't relate to a concrete class instance.
203
+
Los métodos estáticos se utilizan en la funcionalidad propia de la clase "en su conjunto". No se relaciona con una instancia de clase concreta.
206
204
207
-
For example, a method for comparison`Article.compare(article1, article2)`or a factory method`Article.createTodays()`.
205
+
Por ejemplo, un método para comparar`Article.compare(article1, article2)`o un método de fábrica`Article.createTodays()`.
208
206
209
-
They are labeled by the word `static`in class declaration.
207
+
Están etiquetados por la palabra `static`en la declaración de clase.
210
208
211
-
Static properties are used when we'd like to store class-level data, also not bound to an instance.
209
+
Las propiedades estáticas se utilizan cuando queremos almacenar datos a nivel de clase, también no vinculados a una instancia.
212
210
213
-
The syntax is:
211
+
La sintaxis es:
214
212
215
213
```js
216
214
classMyClass {
@@ -222,13 +220,13 @@ class MyClass {
222
220
}
223
221
```
224
222
225
-
Technically, static declaration is the same as assigning to the class itself:
223
+
Técnicamente, la declaración estática es lo mismo que asignar a la clase misma:
226
224
227
225
```js
228
226
MyClass.property=...
229
227
MyClass.method=...
230
228
```
231
229
232
-
Static properties and methods are inherited.
230
+
Las propiedades y métodos estáticos se heredan.
233
231
234
-
For`class B extends A`the prototype of the class`B`itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in`B`, the search continues in`A`.
232
+
Para`class B extends A`el prototipo de la clase`B`en sí mismo apunta a `A`: `B.[[Prototipo]] = A`. Entonces, si no se encuentra un campo en`B`, la búsqueda continúa en`A`.
0 commit comments