Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Prototype methods, objects without __proto__#195

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vplentinax merged 6 commits intojavascript-tutorial:masterfromcortizg:es.javascript.info.1-08-04-pm
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff 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 seaenumerable,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: { // definetoString property
value() { //the value is a function
toString: { // definela 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 adescriptor,its flags are `false`by default. So in the code above, `dictionary.toString`is non-enumerable.
Cuando creamos una propiedad usando undescriptor,sus banderas son `false`por defecto. Entonces, en el código anterior, `dictionary.toString`no esenumerable.

See the the chapter[](info:property-descriptors)for review.
Consulte el capítulo[](info:property-descriptors)para su revisión.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,30 +2,30 @@ importance: 5

---

#Add toStringto the dictionary
#Añadir toStringal 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 adddictionary.toStringmethod
//tu código para agregar el métododictionary.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 toStringin action
alert(dictionary); // "apple,__proto__"
//tu toStringen accion
alert(dictionary); // "manzana,__proto__"
```
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp