-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from cortizg/es.javascript.info.1-08-04-pm
Prototype methods, objects without __proto__
- Loading branch information
Showing
6 changed files
with
129 additions
and
109 deletions.
There are no files selected for viewing
24 changes: 12 additions & 12 deletions
24
1-js/08-prototypes/04-prototype-methods/2-dictionary-tostring/solution.md
This file contains 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 @@ | ||
|
||
The method can take all enumerable keys using `Object.keys` and output their list. | ||
El método puede tomar todas las claves enumerables usando `Object.keys` y generar su lista. | ||
|
||
To make `toString` non-enumerable, let's define it using a property descriptor. The syntax of `Object.create` allows us to provide an object with property descriptors as the second argument. | ||
Para hacer que `toString` no sea enumerable, definámoslo usando un descriptor de propiedad. La sintaxis de `Object.create` nos permite proporcionar un objeto con descriptores de propiedad como segundo argumento. | ||
|
||
```js run | ||
*!* | ||
let dictionary = Object.create(null, { | ||
toString: { // define toString property | ||
value() { // the value is a function | ||
toString: { // define la propiedad toString | ||
value() { // el valor es una funcion | ||
return Object.keys(this).join(); | ||
} | ||
} | ||
}); | ||
*/!* | ||
|
||
dictionary.apple = "Apple"; | ||
dictionary.__proto__ = "test"; | ||
dictionary.apple = "Manzana"; | ||
dictionary.__proto__ = "prueba"; | ||
|
||
// apple and __proto__ is in the loop | ||
// manzana y __proto__ están en el ciclo | ||
for(let key in dictionary) { | ||
alert(key); // "apple", then "__proto__" | ||
alert(key); // "manzana", despues "__proto__" | ||
} | ||
|
||
// comma-separated list of properties by toString | ||
alert(dictionary); // "apple,__proto__" | ||
// lista de propiedades separadas por comas por toString | ||
alert(dictionary); // "manzana,__proto__" | ||
``` | ||
|
||
When we create a property using a descriptor, its flags are `false` by default. So in the code above, `dictionary.toString` is non-enumerable. | ||
Cuando creamos una propiedad usando un descriptor, sus banderas son `false` por defecto. Entonces, en el código anterior, `dictionary.toString` no es enumerable. | ||
|
||
See the the chapter [](info:property-descriptors) for review. | ||
Consulte el capítulo [](info:property-descriptors) para su revisión. |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.