-
Notifications
You must be signed in to change notification settings - Fork 228
Object methods, "this" #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
937e47f
6ea2d55
6f38e54
0893548
cec58a6
12bef1e
39d13f7
fc63d96
f0bfccc
a0a4875
41c48b0
3803ad0
1b97e0f
5b91491
c60d007
066f260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
**Answer: an error.** | ||||||
**Respuesta: un error.** | ||||||
|
||||||
Try it: | ||||||
Pruébalo: | ||||||
```js run | ||||||
function makeUser() { | ||||||
return { | ||||||
|
@@ -11,29 +11,29 @@ function makeUser() { | |||||
|
||||||
let user = makeUser(); | ||||||
|
||||||
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined | ||||||
alert( user.ref.name ); // Error: No se puede leer la propiedad 'name' de undefined | ||||||
``` | ||||||
|
||||||
That's because rules that set `this` do not look at object definition. Only the moment of call matters. | ||||||
Esto es porque porque las reglas que establecen el `this` no buscan en la definición del objeto. Solamente importa el momento en que se llama. | ||||||
joaquinelio marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax. | ||||||
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. | ||||||
|
||||||
The value of `this` is one for the whole function, code blocks and object literals do not affect it. | ||||||
El valor de `this` es uno para la función entera, bloques de código y objetos literales no lo afectan. | ||||||
joaquinelio marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Funciona bien así como dos oraciones independientes en el mismo párrafo y pienso que se entiende mejor. Si lo leo con la coma me voy de corrido y pienso que es "función entera, bloques de código y objetos literales" hasta que mi cerebro hace corto circuito al encontrarse con "no lo afectan". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @maksumi la coma es justamente de pausa en este caso. No se recomiendo usa punto para dar pausa o separar oraciones pequeñas. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. creo qu amerita un " ; " !!! Creo que se lee bien en una oracion si lee con el ritmo adecuado, pero asi no hay duda: podria haber puesto |
||||||
|
||||||
So `ref: this` actually takes current `this` of the function. | ||||||
Entonces `ref: this` en realidad toma el `this` actual de la función. | ||||||
|
||||||
We can rewrite the function and return the same `this` with `undefined` value: | ||||||
Podemos reescribir la función y devolver el mismo `this` con valor `undefined`: | ||||||
|
||||||
```js run | ||||||
function makeUser(){ | ||||||
return this; // this time there's no object literal | ||||||
return this; // esta vez no hay objeto literal | ||||||
} | ||||||
|
||||||
alert( makeUser().name ); // Error: Cannot read property 'name' of undefined | ||||||
alert( makeUser().name ); // Error: No se puede leer la propiedad 'name' de undefined | ||||||
``` | ||||||
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. | ||||||
Como puedes ver el resultado de `alert( makeUser().name )` es el mismo que el resultado de `alert( user.ref.name )` del ejemplo anterior. | ||||||
|
||||||
Here's the opposite case: | ||||||
Aquí está el caso opuesto: | ||||||
|
||||||
```js run | ||||||
function makeUser() { | ||||||
|
@@ -52,4 +52,4 @@ let user = makeUser(); | |||||
alert( user.ref().name ); // John | ||||||
``` | ||||||
|
||||||
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`. | ||||||
Ahora funciona, porque `user.ref()` es un método. Y el valor de `this` es establecido al del objeto delante del punto `.`. |
Uh oh!
There was an error while loading. Please reload this page.