Skip to content

Commit 56ea948

Browse files
author
Maksumi Murakami
authored
Merge pull request #343 from joaquinelio/obmeto
Object methods, "this"
2 parents 9ba6fe3 + 066f260 commit 56ea948

File tree

5 files changed

+105
-106
lines changed

5 files changed

+105
-106
lines changed

1-js/04-object-basics/04-object-methods/4-object-property-this/solution.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
**Answer: an error.**
1+
**Respuesta: un error.**
22

3-
Try it:
3+
Pruébalo:
44
```js run
55
function makeUser() {
66
return {
@@ -11,29 +11,29 @@ function makeUser() {
1111

1212
let user = makeUser();
1313

14-
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
14+
alert( user.ref.name ); // Error: No se puede leer la propiedad 'name' de undefined
1515
```
1616

17-
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
17+
Esto es porque las reglas que establecen el `this` no buscan en la definición del objeto. Solamente importa el momento en que se llama.
1818

19-
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
19+
Aquí el valor de `this` dentro de `makeUser()` es `undefined`, porque es llamado como una función, no como un método con sintaxis de punto.
2020

21-
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
21+
El valor de `this` es uno para la función entera; bloques de código y objetos literales no lo afectan.
2222

23-
So `ref: this` actually takes current `this` of the function.
23+
Entonces `ref: this` en realidad toma el `this` actual de la función.
2424

25-
We can rewrite the function and return the same `this` with `undefined` value:
25+
Podemos reescribir la función y devolver el mismo `this` con valor `undefined`:
2626

2727
```js run
2828
function makeUser(){
29-
return this; // this time there's no object literal
29+
return this; // esta vez no hay objeto literal
3030
}
3131

32-
alert( makeUser().name ); // Error: Cannot read property 'name' of undefined
32+
alert( makeUser().name ); // Error: No se puede leer la propiedad 'name' de undefined
3333
```
34-
As you can see the result of `alert( makeUser().name )` is the same as the result of `alert( user.ref.name )` from the previous example.
34+
Como puedes ver el resultado de `alert( makeUser().name )` es el mismo que el resultado de `alert( user.ref.name )` del ejemplo anterior.
3535

36-
Here's the opposite case:
36+
Aquí está el caso opuesto:
3737

3838
```js run
3939
function makeUser() {
@@ -52,4 +52,4 @@ let user = makeUser();
5252
alert( user.ref().name ); // John
5353
```
5454

55-
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
55+
Ahora funciona, porque `user.ref()` es un método. Y el valor de `this` es establecido al del objeto delante del punto `.`.

1-js/04-object-basics/04-object-methods/4-object-property-this/task.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Using "this" in object literal
5+
# Usando el "this" en un objeto literal
66

7-
Here the function `makeUser` returns an object.
7+
Aquí la función `makeUser` devuelve un objeto.
88

9-
What is the result of accessing its `ref`? Why?
9+
¿Cuál es el resultado de acceder a su `ref`? ¿Por qué?
1010

1111
```js
1212
function makeUser() {
@@ -18,6 +18,5 @@ function makeUser() {
1818

1919
let user = makeUser();
2020

21-
alert( user.ref.name ); // What's the result?
21+
alert( user.ref.name ); // ¿Cuál es el resultado?
2222
```
23-

1-js/04-object-basics/04-object-methods/7-calculator/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Create a calculator
5+
# Crea una calculadora
66

7-
Create an object `calculator` with three methods:
7+
Crea un objeto `calculator` con tres métodos:
88

9-
- `read()` prompts for two values and saves them as object properties.
10-
- `sum()` returns the sum of saved values.
11-
- `mul()` multiplies saved values and returns the result.
9+
- `read()` pide dos valores y los almacena como propiedadess de objeto.
10+
- `sum()` devuelve la suma de los valores almacenados.
11+
- `mul()` multiplica los valores almacenados y devuelve el resultado.
1212

1313
```js
1414
let calculator = {
15-
// ... your code ...
15+
// ... tu código ...
1616
};
1717

1818
calculator.read();

1-js/04-object-basics/04-object-methods/8-chain-calls/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Chaining
5+
# Encadenamiento
66

7-
There's a `ladder` object that allows to go up and down:
7+
Hay un objeto `ladder` que permite subir y bajar:
88

99
```js
1010
let ladder = {
@@ -15,13 +15,13 @@ let ladder = {
1515
down() {
1616
this.step--;
1717
},
18-
showStep: function() { // shows the current step
18+
showStep: function() { // muestra el peldaño actual
1919
alert( this.step );
2020
}
2121
};
2222
```
2323

24-
Now, if we need to make several calls in sequence, can do it like this:
24+
Ahora, si necesitamos hacer varios llamados en secuencia podemos hacer algo como esto:
2525

2626
```js
2727
ladder.up();
@@ -30,10 +30,10 @@ ladder.down();
3030
ladder.showStep(); // 1
3131
```
3232

33-
Modify the code of `up`, `down` and `showStep` to make the calls chainable, like this:
33+
Modifica el código de "arriba" `up`, "abajo" `down` y "mostrar peldaño" `showStep` para hacer los llamados encadenables como esto:
3434

3535
```js
3636
ladder.up().up().down().showStep(); // 1
3737
```
3838

39-
Such approach is widely used across JavaScript libraries.
39+
Tal enfoque es ampliamente usado entre las librerías JavaScript.

0 commit comments

Comments
 (0)