Skip to content

Prototype methods, objects without proto #69

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
merged 2 commits into from
Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

The method can take all enumerable keys using `Object.keys` and output their list.
La méthode peut prendre toutes les clés énumérables en utilisant `Object.keys` et afficher leur liste.

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.
Pour rendre `toString` non-énumérable, définissons-le à l'aide d'un descripteur de propriété. La syntaxe de `Object.create` nous permet de fournir un objet avec des descripteurs de propriété comme second argument.

```js run
*!*
let dictionary = Object.create(null, {
toString: { // define toString property
value() { // the value is a function
toString: { // définir la propriété toString
value() { // la valeur est une fonction
return Object.keys(this).join();
}
}
Expand All @@ -17,15 +17,15 @@ let dictionary = Object.create(null, {
dictionary.apple = "Apple";
dictionary.__proto__ = "test";

// apple and __proto__ is in the loop
// apple et __proto__ sont dans la boucle
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple", puis "__proto__"
}

// comma-separated list of properties by toString
// liste de propriétés séparées par des virgules par toString
alert(dictionary); // "apple,__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.
Lorsque nous créons une propriété à l'aide d'un descripteur, ses indicateurs sont `false` par défaut. Donc, dans le code ci-dessus, `dictionary.toString` est non énumérable.

See the the chapter [](info:property-descriptors) for review.
Voir le chapitre [](info:property-descriptors) pour revoir.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

# Add toString to the dictionary
# Ajouter toString au dictionnaire

There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
Il existe un objet `dictionary`, créé en tant que `Object.create(null)`, pour stocker toutes les paires `clé/valeur`.

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.
Ajoutez la méthode `dictionary.toString()`, qui devrait renvoyer une liste de clés délimitée par des virgules. Votre `toString` ne devrait pas apparaître dans la boucle `for..in` sur l'objet.

Here's how it should work:
Voici comment cela devrait fonctionner:

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

*!*
// your code to add dictionary.toString method
// votre code pour ajouter la méthode dictionary.toString
*/!*

// add some data
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__ is a regular property key here
dictionary.__proto__ = "test"; // __proto__ est une clé de propriété régulière ici

// only apple and __proto__ are in the loop
// seulement apple et __proto__ sont dans la boucle
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple", puis "__proto__"
}

// your toString in action
// votre toString en action
alert(dictionary); // "apple,__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.
Le premier appel a `this==rabbit`, les autres ont `this` égal à `Rabbit.prototype`, car il s'agit en fait de l'objet avant le point.

So only the first call shows `Rabbit`, other ones show `undefined`:
Ainsi, seul le premier appel indique `Rabbit`, les autres affichent` undefined`:

```js run
function Rabbit(name) {
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 différence entre les appels

Let's create a new `rabbit` object:
Créons un nouvel objet `rabbit`:

```js
function Rabbit(name) {
Expand All @@ -17,7 +17,7 @@ Rabbit.prototype.sayHi = function() {
let rabbit = new Rabbit("Rabbit");
```

These calls do the same thing or not?
Ces appels font la même chose ou pas?

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