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

Static properties and methods#183

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
otmon76 merged 1 commit intojavascript-tutorial:masterfromotmon76:1.9.3
Jun 4, 2025
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
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
First, let's see why the latter code doesn't work.
Nejprve se podívejme, proč poslední uvedený kód nefunguje.

The reason becomes obvious if we try to run it. An inheriting class constructor must call`super()`. Otherwise `"this"` won't be "defined".
Důvod bude zřejmý, když se ho pokusíme spustit. Konstruktor zděděné třídy musí volat`super()`, jinak nebude „definováno“ `„this“`.

So here's the fix:
Zde je tedy oprava:

```js run
classRabbit extends Object {
constructor(name) {
classKrálík extends Object {
constructor(jméno) {
*!*
super(); //need to call the parent constructor when inheriting
super(); //při dědění je nutné volat rodičovský konstruktor
*/!*
this.name =name;
this.jméno =jméno;
}
}

letrabbit = newRabbit("Rab");
letkrálík = newKrálík("Bobek");

alert(rabbit.hasOwnProperty('name') ); // true
alert(králík.hasOwnProperty('jméno') ); // true
```

But that's not all yet.
To však ještě není všechno.

Even after the fix, there's still an important difference between `"classRabbit extends Object"` and `classRabbit`.
I po této opravě bude stále existovat důležitý rozdíl mezi `„classKrálík extends Object“` a `classKrálík`.

As we know, the "extends" syntax sets up two prototypes:
Jak víme, syntaxe „extends“ nastavuje dva prototypy:

1.Between `"prototype"`of the constructor functions (for methods).
2.Between the constructor functions themselves (for static methods).
1.Mezi `"prototype"`konstruktorů (pro metody).
2.Mezi samotnými konstruktory (pro statické metody).

In the case of`classRabbit extends Object`it means:
V případě`classKrálík extends Object`to znamená:

```js run
classRabbit extends Object {}
classKrálík extends Object {}

alert(Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
alert(Rabbit.__proto__ === Object ); // (2) true
alert(Králík.prototype.__proto__ === Object.prototype ); // (1) true
alert(Králík.__proto__ === Object ); // (2) true
```

So `Rabbit` now provides access to the static methods of `Object`via `Rabbit`,like this:
`Králík` tedy nyní poskytuje přístup ke statickým metodám třídy `Object`přes třídu `Králík`,například:

```js run
classRabbit extends Object {}
classKrálík extends Object {}

*!*
//normally we call Object.getOwnPropertyNames
alert (Rabbit.getOwnPropertyNames({a: 1, b: 2})); // a,b
//běžně voláme Object.getOwnPropertyNames
alert (Králík.getOwnPropertyNames({a: 1, b: 2})); // a,b
*/!*
```

But if we don't have`extends Object`,then `Rabbit.__proto__`is not set to `Object`.
Jestliže však nemáme`extends Object`,pak se `Králík.__proto__`nenastaví na `Object`.

Here's the demo:
Zde je ukázka:

```js run
classRabbit {}
classKrálík {}

alert(Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
alert(Rabbit.__proto__ === Object ); // (2) false (!)
alert(Rabbit.__proto__ === Function.prototype ); //as any function by default
alert(Králík.prototype.__proto__ === Object.prototype ); // (1) true
alert(Králík.__proto__ === Object ); // (2) false (!)
alert(Králík.__proto__ === Function.prototype ); //jako standardně kterákoli funkce

*!*
//error, no such function in Rabbit
alert (Rabbit.getOwnPropertyNames({a: 1, b: 2})); //Error
//chyba, ve třídě Králík taková funkce není
alert (Králík.getOwnPropertyNames({a: 1, b: 2})); //Chyba
*/!*
```

So `Rabbit` doesn't provide access to static methods of `Object` in that case.
`Králík` tedy v tomto případě neposkytuje přístup ke statickým metodám třídy `Object`.

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`.
Mimochodem, `Function.prototype`obsahuje „obecné“ funkční metody, např. `call`, `bind`atd. Ty jsou definitivně dostupné v obou případech, protože pro zabudovaný konstruktor třídy`Object`platí `Object.__proto__ === Function.prototype`.

Here's the picture:
Zde je obrázek:

![](rabbit-extends-object.svg)

So, toput it short, there are two differences:
Abychom totedy zkrátili, existují dva rozdíly:

| classRabbit | classRabbit extends Object |
| classKrálík | classKrálík extends Object |
|--------------|------------------------------|
| -- |needs to call`super()` in constructor |
| `Rabbit.__proto__ === Function.prototype` | `Rabbit.__proto__ === Object` |
| -- |musí v konstruktoru volat`super()` |
| `Králík.__proto__ === Function.prototype` | `Králík.__proto__ === Object` |
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,41 +2,41 @@ importance: 3

---

#Class extends Object?
#Třída rozšiřuje Object?

As we know, all objects normally inherit from `Object.prototype`and get access to "generic" object methods like `hasOwnProperty`etc.
Jak víme, všechny objekty běžně dědí z `Object.prototype`a získávají přístup k „obecným“ objektovým metodám jako `hasOwnProperty`a podobně.

For instance:
Například:

```js run
classRabbit {
constructor(name) {
this.name =name;
classKrálík {
constructor(jméno) {
this.jméno =jméno;
}
}

letrabbit = newRabbit("Rab");
letkrálík = newKrálík("Bobek");

*!*
// hasOwnPropertymethod is from Object.prototype
alert(rabbit.hasOwnProperty('name') ); // true
//metodahasOwnPropertyje z Object.prototype
alert(králík.hasOwnProperty('jméno') ); // true
*/!*
```

But if we spell it out explicitly like `"classRabbit extends Object"`,then the result would be different from a simple `"classRabbit"`?
Pokud však výslovně uvedeme `„classKrálík extends Object`,bude se výsledek lišit od prostého `„classKrálík“`?

What's the difference?
Jaký je rozdíl?

Here's an example of such code (it doesn't work--why? fix it?):
Zde je příklad takového kódu (nefunguje--proč? opravte ho):

```js
classRabbit extends Object {
constructor(name) {
this.name =name;
classKrálík extends Object {
constructor(jméno) {
this.jméno =jméno;
}
}

letrabbit = newRabbit("Rab");
letkrálík = newKrálík("Bobek");

alert(rabbit.hasOwnProperty('name') ); //Error
alert(králík.hasOwnProperty('jméno') ); //Chyba
```
Loading

[8]ページ先頭

©2009-2025 Movatter.jp