This file contains hidden or 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
First, let's see why the latter code doesn't work.
Lad os først se på, hvorfor den sidste kode ikke virker.
The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined".
Grunden til det bliver tydelig hvis vi prøver at køre den. En arvende klassekonstruktør skal kalde `super()`. Ellers vil `"this"` ikke være "defineret".
So here's the fix:
Så her er en måde at fixe det på:
```js run
class Rabbit extends Object {
constructor(name) {
*!*
super(); // need to call the parent constructor when inheriting
super(); // du skal kalde den overordnede konstruktør når du nedarver
*/!*
this.name = name;
}
Expand All
@@ -19,16 +19,16 @@ let rabbit = new Rabbit("Rab");
alert( rabbit.hasOwnProperty('name') ); // true
```
But that's not all yet.
Men det er ikke alt.
Even after the fix, there's still an important difference between `"class Rabbit extends Object"` and `class Rabbit`.
Selv efter denne rettelse er der en vigtig forskel mellem `"class Rabbit extends Object"` og `class Rabbit`.
As we know, the "extends" syntax sets up two prototypes:
Som vi ved, opretter "extends" syntaksen to prototyper:
1. Between `"prototype"` of the constructor functions (for methods).
2. Between the constructor functions themselves (for static methods).
1. Mellem `"prototype"` af constructor funktionerne (for metoder).
2. Mellem constructor funktionerne selv (for statiske metoder).
In the case of `class Rabbit extends Object` it means:
I tilfældet med `class Rabbit extends Object` betyder det, at `Rabbit` nedarver både de statiske og de normale metoder fra `Object`.:
So `Rabbit` doesn't provide access to static methods of `Object` in that case.
Så `Rabbit` giver ikke adgang til de statiske metoder fra `Object` i det tilfælde.
By the way, `Function.prototype` also has "generic" function methods, like `call`, `bind` etc. They are ultimately available in both cases, because for the built-in `Object` constructor, `Object.__proto__ === Function.prototype`.
Forresten så har`Function.prototype` også en "generiske" function metoder, såsom `call`, `bind` osv. De er i sidste ende tilgængelige i begge tilfælde, fordi for den indbyggede `Object` constructor, `Object.__proto__ === Function.prototype`.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Static properties and methods #267
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
Uh oh!
There was an error while loading. Please reload this page.
Static properties and methods #267
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.