Prototype methods, objects without proto by ockley · Pull Request #264 · javascript-tutorial/da.javascript.info · GitHub
Skip to content
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
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

# Add toString to the dictionary
# Tilføj toString til ordbogen

There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
Der er et objekt `dictionary`, oprettet som `Object.create(null)`, til at gemme enhver `key/value` par.

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.
Tilføj metoden `dictionary.toString()` til det, som skal returnere en komma-separeret liste af nøgler. Din `toString` bør ikke vises i `for..in` over objektet.

Here's how it should work:
Den bør virke sådan her:

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

*!*
// your code to add dictionary.toString method
// din kode til at tilføje dictionary.toString metoden
*/!*

// add some data
dictionary.apple = "Apple";
// tilføj nogle data
dictionary.apple = "æble";
dictionary.__proto__ = "test"; // __proto__ is a regular property key here

// only apple and __proto__ are in the loop
// Kun apple og __proto__ er i loopet
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple" og "__proto__"
}

// your toString in action
// din toString i aktion
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.
Det første kald har `this == rabbit`, de andre har `this` lig med `Rabbit.prototype`, fordi det faktisk er objektet før punktummet.

So only the first call shows `Rabbit`, other ones show `undefined`:
Så kun det første kald viser `Rabbit`, andre viser `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("Kanin");

rabbit.sayHi(); // Rabbit
rabbit.sayHi(); // Kanin
Rabbit.prototype.sayHi(); // undefined
Object.getPrototypeOf(rabbit).sayHi(); // undefined
rabbit.__proto__.sayHi(); // undefined
Expand Down
Loading