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

Constructor, operator "new"#122

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 5 commits intojavascript-tutorial:masterfromotmon76:1.4.5
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,8 +1,8 @@
Yes, it's possible.
Ano, je to možné.

If a function returns an object then`new`returns it instead of `this`.
Jestliže funkce vrací objekt, pak jej`new`vrátí místo `this`.

So they can, for instance, return the same externally defined object `obj`:
Mohou tedy například vrátit stejný externě definovaný objekt `obj`:

```js run no-beautify
let obj = {};
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,9 +2,9 @@ importance: 2

---

#Two functionsone object
#Dvě funkcejeden objekt

Is it possible to create functions`A`and `B`so that `new A() == new B()`?
Je možné vytvořit funkce`A`a `B`tak, aby `new A() == new B()`?

```js no-beautify
function A() { ... }
Expand All@@ -16,4 +16,4 @@ let b = new B();
alert( a == b ); // true
```

If it is, then provide an example of their code.
Pokud ano, uveďte příklad jejich kódu.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
functionCalculator() {
functionKalkulátor() {

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

this.sum = function() {
this.součet = function() {
return this.a + this.b;
};

this.mul = function() {
this.součin = function() {
return this.a * this.b;
};
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@

describe("calculator", function() {
letcalculator;
describe("kalkulátor", function() {
letkalkulátor;
before(function() {
sinon.stub(window, "prompt")

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

calculator = newCalculator();
calculator.read();
kalkulátor = newKalkulátor();
kalkulátor.read();
});

it("the read method asks for two values using promptand remembers them in object properties", function() {
assert.equal(calculator.a, 2);
assert.equal(calculator.b, 3);
it("funkce načti se zeptá na dvě hodnoty pomocí prompta zapamatuje si je jako vlastnosti objektu", function() {
assert.equal(kalkulátor.a, 2);
assert.equal(kalkulátor.b, 3);
});

it("when 2 and 3 are entered, the sum is 5", function() {
assert.equal(calculator.sum(), 5);
it("když zadáme 2 a 3, součet je 5", function() {
assert.equal(kalkulátor.součet(), 5);
});

it("when 2 and 3 are entered, the product is 6", function() {
assert.equal(calculator.mul(), 6);
it("když zadáme 2 a 3, součin je 6", function() {
assert.equal(kalkulátor.součin(), 6);
});

after(function() {
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
```js run demo
functionCalculator() {
functionKalkulátor() {

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

this.sum = function() {
this.součet = function() {
return this.a + this.b;
};

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

letcalculator = newCalculator();
calculator.read();
letkalkulátor = newKalkulátor();
kalkulátor.načti();

alert( "Sum=" +calculator.sum() );
alert( "Mul=" +calculator.mul() );
alert( "Součet=" +kalkulátor.součet() );
alert( "Součin=" +kalkulátor.součin() );
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,22 +2,22 @@ importance: 5

---

#Create new Calculator
#Vytvořte nový Kalkulátor

Create a constructor function `Calculator` that creates objects with 3 methods:
Vytvořte konstruktor `Kalkulátor`, který bude vytvářet objekty 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 these properties.
- `mul()`returns the multiplication product of these properties.
- `načti()`se funkcí `prompt` zeptá na dvě hodnoty a uloží je jako vlastnosti objektu s názvy po řadě`a`a `b`.
- `součet()`vrátí součet těchto hodnot.
- `součin()`vrátí součin těchto hodnot.

For instance:
Například:

```js
letcalculator = newCalculator();
calculator.read();
letkalkulátor = newKalkulátor();
kalkulátor.načti();

alert( "Sum=" +calculator.sum() );
alert( "Mul=" +calculator.mul() );
alert( "Součet=" +kalkulátor.součet() );
alert( "Součin=" +kalkulátor.součin() );
```

[demo]
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
functionAccumulator(startingValue) {
this.value =startingValue;
functionAkumulátor(počátečníHodnota) {
this.hodnota =počátečníHodnota;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.načti = function() {
this.hodnota += +prompt('Kolik přičíst?', 0);
};

}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
describe("Accumulator", function() {
describe("Akumulátor", function() {

beforeEach(function() {
sinon.stub(window, "prompt")
Expand All@@ -8,23 +8,23 @@ describe("Accumulator", function() {
prompt.restore();
});

it("initial value is theargumentof the constructor", function() {
letaccumulator = newAccumulator(1);
it("úvodní hodnota jeargumentkonstruktoru", function() {
letakumulátor = newAkumulátor(1);

assert.equal(accumulator.value, 1);
assert.equal(akumulátor.hodnota, 1);
});

it("after reading 0, the value is 1", function() {
letaccumulator = newAccumulator(1);
it("po načtení 0 je hodnota 1", function() {
letakumulátor = newAkumulátor(1);
prompt.returns("0");
accumulator.read();
assert.equal(accumulator.value, 1);
akumulátor.načti();
assert.equal(akumulátor.hodnota, 1);
});

it("after reading 1, the value is 2", function() {
letaccumulator = newAccumulator(1);
it("po načtení 1 je hodnota 2", function() {
letakumulátor = newAkumulátor(1);
prompt.returns("1");
accumulator.read();
assert.equal(accumulator.value, 2);
akumulátor.načti();
assert.equal(akumulátor.hodnota, 2);
});
});
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@


```js run demo
functionAccumulator(startingValue) {
this.value =startingValue;
functionAkumulátor(počátečníHodnota) {
this.hodnota =počátečníHodnota;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.načti = function() {
this.hodnota += +prompt('Kolik přičíst?', 0);
};

}

letaccumulator = newAccumulator(1);
accumulator.read();
accumulator.read();
alert(accumulator.value);
letakumulátor = newAkumulátor(1);
akumulátor.načti();
akumulátor.načti();
alert(akumulátor.hodnota);
```
22 changes: 11 additions & 11 deletions1-js/04-object-basics/06-constructor-new/3-accumulator/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,26 +2,26 @@ importance: 5

---

#Create new Accumulator
#Vytvořte nový Akumulátor

Create a constructor function `Accumulator(startingValue)`.
Vytvořte konstruktor `Akumulátor(počátečníHodnota)`.

Object that it creates should:
Objekt, který je tímto konstruktorem vytvořen, by měl:

-Store the "current value" in the property `value`.The starting value is set to theargumentof the constructor `startingValue`.
-The `read()`method should use`prompt`to read a new number and add it to `value`.
-Ukládat „aktuální hodnotu“ do vlastnosti `hodnota`.Počáteční hodnota se nastaví naargumentkonstruktoru `počátečníHodnota`.
-Metoda `načti()`by se měla pomocí`prompt`zeptat na nové číslo a přičíst je k vlastnosti `hodnota`.

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
Jinými slovy, vlastnost `hodnota` bude součet všech uživatelem zadaných hodnot a úvodní hodnoty `počátečníHodnota`.

Here's the demo of the code:
Zde je ukázka kódu:

```js
letaccumulator = newAccumulator(1); //initial value 1
letakumulátor = newAkumulátor(1); //počáteční hodnota 1

accumulator.read(); //adds the user-entered value
accumulator.read(); //adds the user-entered value
akumulátor.načti(); //přičte uživatelem zadanou hodnotu
akumulátor.načti(); //přičte uživatelem zadanou hodnotu

alert(accumulator.value); //shows the sum of these values
alert(akumulátor.hodnota); //zobrazí součet těchto hodnot
```

[demo]
Loading

[8]ページ先頭

©2009-2025 Movatter.jp