Constructor, operator "new" by ockley · Pull Request #229 · 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,9 +2,9 @@ importance: 2

---

# Two functions – one object
# To funktioner – ét objekt

Is it possible to create functions `A` and `B` so that `new A() == new B()`?
Er det muligt at skabe funktionerne `A` og `B`, så `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.
Hvis det er tilfældet, så giv et eksempel på deres kode.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

describe("calculator", function() {
describe("lommeregner", function () {
let calculator;
before(function() {
before(function () {
sinon.stub(window, "prompt")

prompt.onCall(0).returns("2");
Expand All @@ -10,21 +10,21 @@ describe("calculator", function() {
calculator = new Calculator();
calculator.read();
});
it("the read method asks for two values using prompt and remembers them in object properties", function() {

it("read-metoden spørger efter to værdier ved hjælp af prompt og husker dem i objektets egenskaber", function () {
assert.equal(calculator.a, 2);
assert.equal(calculator.b, 3);
});

it("when 2 and 3 are entered, the sum is 5", function() {
it("når 2 og 3 indtastes, er summen 5", function () {
assert.equal(calculator.sum(), 5);
});

it("when 2 and 3 are entered, the product is 6", function() {
it("når 2 og 3 indtastes, er produktet 6", function () {
assert.equal(calculator.mul(), 6);
});

after(function() {
after(function () {
prompt.restore();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ importance: 5

---

# Create new Calculator
# Opret en ny lommeregner

Create a constructor function `Calculator` that creates objects with 3 methods:
Opret en konstruktørfunktion `Calculator`, der skaber objekter med 3 metoder:

- `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.
- `read()` spørger efter to værdier og gemmer dem som egenskaber med navnene `a` og `b`.
- `sum()` returnerer summen af disse egenskaber.
- `mul()` returnerer produktet af disse egenskaber.

For instance:
For eksempel:

```js
let calculator = new Calculator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Hvor meget vil du tilføje?', 0);
};

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
describe("Accumulator", function() {
describe("Accumulator", function () {

beforeEach(function() {
beforeEach(function () {
sinon.stub(window, "prompt")
});

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

it("initial value is the argument of the constructor", function() {
it("initialiseret værdi er argumentet for konstruktøren", function () {
let accumulator = new Accumulator(1);

assert.equal(accumulator.value, 1);
});

it("after reading 0, the value is 1", function() {
it("efter at have læst 0, er værdien 1", function () {
let accumulator = new Accumulator(1);
prompt.returns("0");
accumulator.read();
assert.equal(accumulator.value, 1);
});

it("after reading 1, the value is 2", function() {
it("efter at have læst 1, er værdien 2", function () {
let accumulator = new Accumulator(1);
prompt.returns("1");
accumulator.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Hvor meget vil du tilføje?', 0);
};

}
Expand Down
22 changes: 11 additions & 11 deletions 1-js/04-object-basics/06-constructor-new/3-accumulator/task.md
Loading