Skip to content
Open
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
@@ -1,9 +1,8 @@
Yes, it's possible.
Vâng, nó có thể.

If a function returns an object then `new` returns it instead of `this`.

So they can, for instance, return the same externally defined object `obj`:
Nếu một hàm trả về một đối tượng thì `new` sẽ trả về nó thay vì `this`.

Vì vậy, chẳng hạn, chúng có thể trả về cùng một đối tượng được xác định bên ngoài `obj`:
```js run no-beautify
let obj = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 2

---

# Two functionsone object
# Hai hàmmột đối tượng

Is it possible to create functions `A` and `B` such as `new A()==new B()`?
Có thể tạo các hàm `A` `B` sao cho `new A() == new B()` không?

```js no-beautify
function A() { ... }
function B() { ... }

let a = new A;
let b = new B;
let a = new A();
let b = new B();

alert( a == b ); // true
```

If it is, then provide an example of their code.
Nếu có, thì hãy cung cấp một ví dụ về mã của chúng.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

describe("calculator", function() {
describe("máy tính", function() {
let calculator;
before(function() {
sinon.stub(window, "prompt")
sinon.stub(window, "nhắc")

prompt.onCall(0).returns("2");
prompt.onCall(1).returns("3");
Expand All @@ -11,16 +11,16 @@ describe("calculator", function() {
calculator.read();
});

it("the read method asks for two values using prompt and remembers them in object properties", function() {
it("phương thức đọc yêu cầu hai giá trị bằng nhắc và ghi nhớ chúng trong thuộc tính đối tượng", function() {
assert.equal(calculator.a, 2);
assert.equal(calculator.b, 3);
});

it("when 2 and 3 are entered, the sum is 5", function() {
it("khi nhập vào 2 và 3, tổng là 5", function() {
assert.equal(calculator.sum(), 5);
});

it("when 2 and 3 are entered, the product is 6", function() {
it("khi nhập vào 2 và 3, tích là 6", function() {
assert.equal(calculator.mul(), 6);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ function Calculator() {
let calculator = new Calculator();
calculator.read();

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );
alert( "Tổng=" + calculator.sum() );
alert( "Tích=" + calculator.mul() );
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ importance: 5

---

# Create new Calculator
# Tạo một Máy tính mới

Create a constructor function `Calculator` that creates objects with 3 methods:
Tạo một constructor `Calculator` để tạo đối tượng với 3 phương thức:

- `read()` asks for two values using `prompt` and remembers them in object properties.
- `sum()` returns the sum of these properties.
- `mul()` returns the multiplication product of these properties.

For instance:
- `read()` nhắc hai giá trị và lưu chúng dưới dạng thuộc tính đối tượng với tên `a` và `b` tương ứng.
- `sum()` trả về tổng của các thuộc tính này.
- `mul()` trả về tích nhân của các thuộc tính này.

```js
let calculator = new Calculator();
calculator.read();

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );
alert( "Tổng=" + calculator.sum() );
alert( "Tích=" + calculator.mul() );
```

[demo]
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('Cộng vào bao nhiêu?', 0);
};

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
describe("Accumulator", function() {
describe("Bộ tích luỹ", function() {

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

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

it("initial value is the argument of the constructor", function() {
it("giá trị ban đầu là đối số của hàm tạo", function() {
let accumulator = new Accumulator(1);

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

it("after reading 0, the value is 1", function() {
it("sau khi đọc 0, giá trị là 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("sau khi đọc 1, giá trị là 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('Cộng vào bao nhiêu?', 0);
};

}
Expand Down
22 changes: 11 additions & 11 deletions 1-js/04-object-basics/06-constructor-new/3-accumulator/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ importance: 5

---

# Create new Accumulator
# Tạo bộ tích lũy mới

Create a constructor function `Accumulator(startingValue)`.
Tạo một constructor `Accumulator(startingValue)`.

Object that it creates should:
Đối tượng mà nó tạo nên:

- Store the "current value" in the property `value`. The starting value is set to the argument of the constructor `startingValue`.
- The `read()` method should use `prompt` to read a new number and add it to `value`.
- Lưu trữ "giá trị hiện tại" trong thuộc tính `value`. Giá trị bắt đầu được đặt thành đối số của constructor `startingValue`.
- Phương thức `read()` nên sử dụng `prompt` để đọc một số mới và thêm nó vào `giá trị`.

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
Nói cách khác, thuộc tính `value` là tổng của tất cả các giá trị do người dùng nhập với giá trị ban đầu là `startingValue`.

Here's the demo of the code:
Đây là bản demo của mã:

```js
let accumulator = new Accumulator(1); // initial value 1
let accumulator = new Accumulator(1); // giá trị ban đầu 1

accumulator.read(); // adds the user-entered value
accumulator.read(); // adds the user-entered value
accumulator.read(); // thêm giá trị do người dùng nhập
accumulator.read(); // thêm giá trị do người dùng nhập

alert(accumulator.value); // shows the sum of these values
alert(accumulator.value); // hiển thị tổng của các giá trị này
```

[demo]
Loading