Skip to content

Commit

Permalink
Merge pull request #195 from cortizg/es.javascript.info.1-08-04-pm
Browse files Browse the repository at this point in the history
Prototype methods, objects without __proto__
  • Loading branch information
vplentinax committed Jun 3, 2020
2 parents 417b7f8 + c1a6bfb commit 39af0cc
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 109 deletions.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

# Add toString to the dictionary
# Añadir toString al diccionario

There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
Hay un objeto `dictionary`, creado como `Object.create(null)`, para almacenar cualquier par `clave/valor`.

Add method `dictionary.toString()` into it, that should return a comma-delimited list of keys. Your `toString` should not show up in `for..in` over the object.
Agrega el método `dictionary.toString()`, que debería devolver una lista de claves delimitadas por comas. Tu `toString` no debe aparecer al iterar un `for..in` sobre el objeto.

Here's how it should work:
Así es como debería funcionar:

```js
let dictionary = Object.create(null);

*!*
// your code to add dictionary.toString method
// tu código para agregar el método dictionary.toString
*/!*

// add some data
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__ is a regular property key here
// agregar algunos datos
dictionary.apple = "Manzana";
dictionary.__proto__ = "prueba"; // // aquí proto es una propiedad clave común

// only apple and __proto__ are in the loop
// solo manzana y __proto__ están en el ciclo
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "manzana", despues "__proto__"
}

// your toString in action
alert(dictionary); // "apple,__proto__"
// tu toString en accion
alert(dictionary); // "manzana,__proto__"
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

The first call has `this == rabbit`, the other ones have `this` equal to `Rabbit.prototype`, because it's actually the object before the dot.
La primera llamada tiene `this == rabbit`, las otras tienen `this` igual a `Rabbit.prototype`, porque en realidad es el objeto antes del punto.

So only the first call shows `Rabbit`, other ones show `undefined`:
Entonces, solo la primera llamada muestra `Rabbit`, las otras muestran `undefined`:

```js run
function Rabbit(name) {
Expand All @@ -11,9 +11,9 @@ Rabbit.prototype.sayHi = function() {
alert( this.name );
}

let rabbit = new Rabbit("Rabbit");
let rabbit = new Rabbit("Conejo");

rabbit.sayHi(); // Rabbit
rabbit.sayHi(); // Conejo
Rabbit.prototype.sayHi(); // undefined
Object.getPrototypeOf(rabbit).sayHi(); // undefined
rabbit.__proto__.sayHi(); // undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The difference between calls
# La diferencia entre llamadas

Let's create a new `rabbit` object:
Creemos un nuevo objeto `rabbit`:

```js
function Rabbit(name) {
Expand All @@ -14,10 +14,10 @@ Rabbit.prototype.sayHi = function() {
alert(this.name);
};

let rabbit = new Rabbit("Rabbit");
let rabbit = new Rabbit("Conejo");
```

These calls do the same thing or not?
Estas llamadas hacen lo mismo o no?

```js
rabbit.sayHi();
Expand Down
Loading

0 comments on commit 39af0cc

Please sign in to comment.