- Notifications
You must be signed in to change notification settings - Fork 231
Property getters and setters#194
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
Merged
joaquinelio merged 14 commits into javascript-tutorial:master from rainvare:rainvare-patch-1Jun 30, 2020
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
84d9a86 Update article.md
rainvare 62996fc Merge branch 'master' into rainvare-patch-1
joaquinelio 05a92e5 Update article.md
rainvare f9b2439 Update article.md
rainvare 78810a6 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare 1ddc056 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare e9b0519 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare 0988760 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare 3cee180 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare c19bdf8 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare 67303a9 Update 1-js/07-object-properties/02-property-accessors/article.md
rainvare 0cb05bb Update article.md
rainvare fc70112 Update 1-js/07-object-properties/02-property-accessors/article.md
joaquinelio 039e27b Update 1-js/07-object-properties/02-property-accessors/article.md
joaquinelio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
85 changes: 43 additions & 42 deletions 85 1-js/07-object-properties/02-property-accessors/article.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,31 +1,31 @@ | ||||||
| # Property getters and setters | ||||||
| There are two kinds of properties. | ||||||
| Hay dos tipos de propiedades de objetos. | ||||||
| The first kind is *data properties*. We already know how to work with them. All properties that we've been using until now were data properties. | ||||||
| El primer tipo son las *propiedades de los datos*. Ya sabemos cómo trabajar con ellas. En realidad, todas las propiedades que hemos estado usando hasta ahora eran propiedades de datos. | ||||||
| The second type of properties is something new. It's *accessor properties*. They are essentially functions that work on getting and setting a value, but look like regular properties to an external code. | ||||||
| El segundo tipo de propiedades es algo nuevo. Son las *propiedades de acceso*. Estas son esencialmente funciones que se ejecutan para la obtención y asignación de un valor, pero parecen propiedades normales para un código externo. | ||||||
| ## Getters and setters | ||||||
| Accessor properties are represented by "getter" and "setter" methods. In an object literal they are denoted by `get` and `set`: | ||||||
| Las propiedades de acceso están representadas por métodos "getter" y "setter". Propiamente, en un objeto se denotan por `get` y `set`: | ||||||
| ```js | ||||||
| let obj ={ | ||||||
| *!*get propName()*/!*{ | ||||||
| // getter, the code executed on getting obj.propName | ||||||
| // getter, el código ejecutado para obtener obj.propName | ||||||
| }, | ||||||
| *!*set propName(value)*/!*{ | ||||||
| // setter, the code executed on setting obj.propName = value | ||||||
| // setter, el código ejecutado para asignar obj.propName = value | ||||||
| } | ||||||
| }; | ||||||
| ``` | ||||||
| The getter works when `obj.propName` is read, the setter -- when it is assigned. | ||||||
| El getter funciona cuando se lee `obj.propName`, el setter -- cuando se asigna. | ||||||
| For instance, we have a `user` object with `name` and `surname`: | ||||||
| Por ejemplo, tenemos un objeto "usuario" con "nombre" y "apellido": | ||||||
| ```js | ||||||
| let user ={ | ||||||
| @@ -34,7 +34,7 @@ let user ={ | ||||||
| }; | ||||||
| ``` | ||||||
| Now we want to add a `fullName` property, that should be `"John Smith"`. Of course, we don't want to copy-paste existing information, so we can implement it as an accessor: | ||||||
| Ahora queremos añadir una propiedad de "Nombre completo" (`fullName`), que debería ser `"John Smith"`. Por supuesto, no queremos copiar-pegar la información existente, así que podemos aplicarla como una propiedad de acceso: | ||||||
| ```js run | ||||||
| let user ={ | ||||||
| @@ -53,9 +53,9 @@ alert(user.fullName); // John Smith | ||||||
| */!* | ||||||
| ``` | ||||||
| From outside, an accessor property looks like a regular one. That's the idea of accessor properties. We don't *call* `user.fullName` as a function, we *read* it normally: the getter runs behind the scenes. | ||||||
| Desde fuera, una propiedad de acceso se parece a una normal. Esa es la idea de estas propiedades. No *llamamos* a `user.fullName` como una función, la *leemos* normalmente: el "getter" corre detrás de la escena. | ||||||
| As of now, `fullName` has only a getter. If we attempt to assign `user.fullName=`, there will be an error: | ||||||
| A partir de ahora, "Nombre completo" sólo tiene un receptor. Si intentamos asignar `user.fullName=`, habrá un error. | ||||||
| ```js run | ||||||
| let user ={ | ||||||
| @@ -69,7 +69,7 @@ user.fullName = "Test" // Error (property has only a getter) | ||||||
| */!* | ||||||
| ``` | ||||||
| Let's fix it by adding a setter for `user.fullName`: | ||||||
| Arreglémoslo agregando un setter para `user.fullName`: | ||||||
| ```js run | ||||||
| let user ={ | ||||||
| @@ -87,29 +87,30 @@ let user ={ | ||||||
| */!* | ||||||
| }; | ||||||
| // set fullName is executed with the given value. | ||||||
| // set fullName se ejecuta con el valor dado. | ||||||
| user.fullName = "Alice Cooper" | ||||||
| alert(user.name); // Alice | ||||||
| alert(user.surname); // Cooper | ||||||
| ``` | ||||||
| As the result, we have a "virtual" property `fullName`. It is readable and writable. | ||||||
| Como resultado, tenemos una propiedad virtual `fullName` que puede leerse y escribirse. | ||||||
| ## Accessor descriptors | ||||||
| Descriptors for accessor properties are different from those for data properties. | ||||||
| Los descriptores de las propiedades de acceso son diferentes de aquellos para las propiedades de los datos. | ||||||
| For accessor properties, there is no `value` or `writable`, but instead there are `get` and `set` functions. | ||||||
| Para las propiedades de acceso, no hay cosas como "valor" y "escritura", sino de "get" y "set". | ||||||
| That is, an accessor descriptor may have: | ||||||
| Así que un descriptor de accesos puede tener: | ||||||
| - **`get`** -- a function without arguments, that works when a property is read, | ||||||
| - **`set`** -- a function with one argument, that is called when the property is set, | ||||||
| - **`enumerable`** -- same as for data properties, | ||||||
| - **`configurable`** -- same as for data properties. | ||||||
| - **`get`** -- una función sin argumentos, que funciona cuando se lee una propiedad, | ||||||
| - **`set`** -- una función con un argumento, que se llama cuando se establece la propiedad, | ||||||
| - **`enumerable`** -- lo mismo que para las propiedades de los datos, | ||||||
| - **`configurable`** -- lo mismo que para las propiedades de los datos. | ||||||
| For instance, to create an accessor `fullName` with `defineProperty`, we can pass a descriptor with `get` and `set`: | ||||||
| Por ejemplo, para crear un acceso " Nombre Completo" con "Definir Propiedad", podemos pasar un descriptor con `get` y `set`: | ||||||
Contributor
| ||||||
| Por ejemplo, para crear un acceso " Nombre Completo" con "Definir Propiedad", podemos pasar un descriptor con `get` y `set`: | |
| Por ejemplo, para crear un acceso `fullName`con `defineProperty`, podemos pasar un descriptor con `get` y `set`: |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.