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

Object methods, "this"#150

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.4.4
Jun 3, 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
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
**Answer: an error.**
**Odpověď: chyba.**

Try it:
Zkuste to:
```js run
functionmakeUser() {
functionvytvořUživatele() {
return {
name: "John",
ref: this
jméno: "Jan",
odkaz: this
};
}

letuser =makeUser();
letuživatel =vytvořUživatele();

alert(user.ref.name ); //Error: Cannot read property 'name' of undefined
alert(uživatel.odkaz.jméno ); //Chyba: Nelze načíst vlastnost 'jméno' objektu undefined
```

That's because rules that set`this` do not look at object definition. Only the moment of call matters.
Je to proto, že pravidla, která nastavují`this`, se nedívají do definice objektu. Záleží jen na okamžiku volání.

Here the value of`this`inside `makeUser()`is`undefined`,because it is called as a function, not as a method with "dot" syntax.
Zde je hodnota`this`uvnitř `vytvořUživatele()` `undefined`,protože tato funkce je volána jako funkce, ne jako metoda „tečkovou“ syntaxí.

The value of`this`is one for the whole function, code blocks and object literals do not affect it.
Hodnota`this`je stejná pro celou funkci, bloky kódu a objektové literály ji neovlivňují.

So `ref: this`actually takes current`this`of the function.
Takže `odkaz: this`vezme ve skutečnosti aktuální`this`této funkce.

We can rewrite the function and return the same`this`with`undefined` value:
Můžeme funkci přepsat a vrátit stejné`this`s hodnotou`undefined`:

```js run
functionmakeUser(){
return this; //this time there's no object literal
functionvytvořUživatele(){
return this; //tentokrát tady není objektový literál
}

alert(makeUser().name ); //Error: Cannot read property 'name' of undefined
alert(vytvořUživatele().jméno ); //Chyba: Nelze načíst vlastnost 'jméno' objektu undefined
```
As you can see the result of`alert(makeUser().name )`is the same as the result of`alert(user.ref.name )`from the previous example.
Jak vidíte, výsledek`alert(vytvořUživatele().jméno )`je stejný jako výsledek`alert(uživatel.odkaz.jméno )`z předchozího příkladu.

Here's the opposite case:
Toto je opačný příklad:

```js run
functionmakeUser() {
functionvytvořUživatele() {
return {
name: "John",
jméno: "Jan",
*!*
ref() {
odkaz() {
return this;
}
*/!*
};
}

letuser =makeUser();
letuživatel =vytvořUživatele();

alert(user.ref().name ); //John
alert(uživatel.odkaz().jméno ); //Jan
```

Now it works, because `user.ref()`is a method. And the value of`this`is set to the object before dot `.`.
Teď to funguje, protože `uživatel.odkaz()`je metoda. A hodnota`this`se nastaví na objekt před tečkou `.`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,22 +2,22 @@ importance: 5

---

#Using "this" in object literal
#Použití „this“ v objektovém literálu

Here the function `makeUser` returns an object.
Zde funkce `vytvořUživatele` vrátí objekt.

What is the result of accessing its `ref`?Why?
Jaký je výsledek přístupu k jeho vlastnosti `odkaz`?Proč?

```js
functionmakeUser() {
functionvytvořUživatele() {
return {
name: "John",
ref: this
jméno: "Jan",
odkaz: this
};
}

letuser =makeUser();
letuživatel =vytvořUživatele();

alert(user.ref.name ); //What's the result?
alert(uživatel.odkaz.jméno ); //Jaký je výsledek?
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
letcalculator = {
sum() {
letkalkulátor = {
součet() {
return this.a + this.b;
},

mul() {
součin() {
return this.a * this.b;
},

read() {
načti() {
this.a = +prompt('a?', 0);
this.b = +prompt('b?', 0);
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@


describe("calculator", function() {
describe("kalkulátor", function() {

context("when 2 and 3 entered", function() {
context("když zadáme 2 a 3", function() {
beforeEach(function() {
sinon.stub(window, "prompt");

prompt.onCall(0).returns("2");
prompt.onCall(1).returns("3");

calculator.read();
kalkulátor.načti();
});

afterEach(function() {
prompt.restore();
});

it('the read get two values and saves them as object properties', function () {
assert.equal(calculator.a, 2);
assert.equal(calculator.b, 3);
it('funkce načti načte dvě hodnoty a uloží je jako vlastnosti objektu', function () {
assert.equal(kalkulátor.a, 2);
assert.equal(kalkulátor.b, 3);
});

it("the sum is 5", function() {
assert.equal(calculator.sum(), 5);
it("součet je 5", function() {
assert.equal(kalkulátor.součet(), 5);
});

it("the multiplication product is 6", function() {
assert.equal(calculator.mul(), 6);
it("součin je 6", function() {
assert.equal(kalkulátor.součin(), 6);
});
});

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@

```js run demo solution
letcalculator = {
sum() {
letkalkulátor = {
součet() {
return this.a + this.b;
},

mul() {
součin() {
return this.a * this.b;
},

read() {
načti() {
this.a = +prompt('a?', 0);
this.b = +prompt('b?', 0);
}
};

calculator.read();
alert(calculator.sum() );
alert(calculator.mul() );
kalkulátor.načti();
alert(kalkulátor.součet() );
alert(kalkulátor.součin() );
```
20 changes: 10 additions & 10 deletions1-js/04-object-basics/04-object-methods/7-calculator/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,22 +2,22 @@ importance: 5

---

#Create a calculator
#Vytvořte kalkulátor

Create an object `calculator` with three methods:
Vytvořte objekt `kalkulátor` se třemi metodami:

- `read()`prompts for two values and saves them as object properties with names`a`and `b` respectively.
- `sum()`returns the sum of saved values.
- `mul()`multiplies saved values and returns the result.
- `načti()`se zeptá na dvě hodnoty a uloží je jako vlastnosti objektu pod názvy po řadě`a`a `b`.
- `součet()`vrátí součet uložených hodnot.
- `součin()`vynásobí uložené hodnoty mezi sebou a vrátí výsledek.

```js
letcalculator = {
// ...your code ...
letkalkulátor = {
// ...váš kód ...
};

calculator.read();
alert(calculator.sum() );
alert(calculator.mul() );
kalkulátor.načti();
alert(kalkulátor.součet() );
alert(kalkulátor.součin() );
```

[demo]
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@

letladder = {
step: 0,
up: function() {
this.step++;
letžebřík = {
stupeň: 0,
nahoru: function() {
this.stupeň++;
return this;
},
down: function() {
this.step--;
dolů: function() {
this.stupeň--;
return this;
},
<<<<<<< HEAD
zobrazStupeň: function() {
alert(this.stupeň);
=======
showStep: function() {
alert(this.step);
return this;
>>>>>>> 71da17e5960f1c76aad0d04d21f10bc65318d3f6
}
};
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@

describe('Ladder', function() {
describe('Žebřík', function() {
before(function() {
window.alert = sinon.stub(window, "alert");
});

beforeEach(function() {
ladder.step = 0;
žebřík.stupeň = 0;
});

it('up()should return this', function() {
assert.equal(ladder.up(),ladder);
it('nahoru()by mělo vrátit this', function() {
assert.equal(žebřík.nahoru(),žebřík);
});

it('down()should return this', function() {
assert.equal(ladder.down(),ladder);
it('dolů()by mělo vrátit this', function() {
assert.equal(žebřík.dolů(),žebřík);
});

it('showStep()should call alert', function() {
ladder.showStep();
it('zobrazStupeň()by mělo volat alert', function() {
žebřík.zobrazStupeň();
assert(alert.called);
});

it('up()should increase step', function() {
assert.equal(ladder.up().up().step, 2);
it('nahoru()by mělo zvýšit stupeň', function() {
assert.equal(žebřík.nahoru().nahoru().stupeň, 2);
});

it('down()should decrease step', function() {
assert.equal(ladder.down().step, -1);
it('dolů()by mělo snížit stupeň', function() {
assert.equal(žebřík.dolů().stupeň, -1);
});

it('down().up().up().up() ', function() {
assert.equal(ladder.down().up().up().up().step, 2);
it('dolů().nahoru().nahoru().nahoru() ', function() {
assert.equal(žebřík.dolů().nahoru().nahoru().nahoru().stupeň, 2);
});

it('showStep() should return this', function() {
Expand All@@ -42,7 +42,7 @@ describe('Ladder', function() {
});

after(function() {
ladder.step = 0;
žebřík.stupeň = 0;
alert.restore();
});
});
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
The solution is to return the object itself from every call.
Řešením je v každém volání vrátit tento objekt samotný.

```js run demo
letladder = {
step: 0,
up() {
this.step++;
letžebřík = {
stupeň: 0,
nahoru() {
this.stupeň++;
*!*
return this;
*/!*
},
down() {
this.step--;
dolů() {
this.stupeň--;
*!*
return this;
*/!*
},
showStep() {
alert( this.step );
zobrazStupeň() {
alert( this.stupeň );
*!*
return this;
*/!*
}
};

ladder.up().up().down().showStep().down().showStep(); //shows 1 then 0
žebřík.nahoru().nahoru().dolů().zobrazStupeň().dolů().zobrazStupeň(); //zobrazí 1, pak 0
```

We also can write a single call per line. For long chains it's more readable:
Můžeme také psát každé volání na nový řádek. U delšího zřetězení je to čitelnější:

```js
ladder
.up()
.up()
.down()
.showStep() // 1
.down()
.showStep(); // 0
žebřík
.nahoru()
.nahoru()
.dolů()
.zobrazStupeň() // 1
.dolů()
.zobrazStupeň(); // 0
```
Loading

[8]ページ先頭

©2009-2025 Movatter.jp