Skip to content

Constructor, operator "new" #400

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
@@ -1,8 +1,8 @@
Yes, it's possible.
Sim, é possível.

If a function returns an object then `new` returns it instead of `this`.
Se uma função retorna um objeto, então `new` o retorna ao invés de `this`.

So they can, for instance, return the same externally defined object `obj`:
Então elas podem, por exemplo, retornar o mesmo objeto `obj` definido externamente:

```js run no-beautify
let obj = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 2

---

# Two functionsone object
# Duas funçõesum objeto

Is it possible to create functions `A` and `B` so that `new A() == new B()`?
É possível criar funções `A` e `B` tais que `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.
Se sim, então forneça um exemplo de seus códigos.
Original file line number Diff line number Diff line change
Expand Up @@ -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("o método read solicita por 2 valores usando o prompt e os guarda em propriedades do objeto", function() {
assert.equal(calculator.a, 2);
assert.equal(calculator.b, 3);
});

it("when 2 and 3 are entered, the sum is 5", function() {
it("quando 2 e 3 são inseridos, a soma é 5", function() {
assert.equal(calculator.sum(), 5);
});

it("when 2 and 3 are entered, the product is 6", function() {
it("quando 2 e 3 são inseridos, o produto é 6", function() {
assert.equal(calculator.mul(), 6);
});

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

---

# Create new Calculator
# Crie new Calculator

Create a constructor function `Calculator` that creates objects with 3 methods:
Crie uma função construtora `Calculator` que crie objetos com 3 métodos:

- `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()` solicita 2 valores e os guarda como propriedades do objeto com nomes `a` e `b` respectivamente.
- `sum()` retorna a soma dessas propriedades.
- `mul()` retorna o produto da multiplicação dessas propriedades.

For instance:
Por exemplo:

```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('Quanto adicionar?', 0);
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ describe("Accumulator", function() {
prompt.restore();
});

it("initial value is the argument of the constructor", function() {
it("o valor inicial é o argumento do construtor", function() {
let accumulator = new Accumulator(1);

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

it("after reading 0, the value is 1", function() {
it("após ler 0, o valor é 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("após ler 1, o valor é 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 @@ -3,9 +3,9 @@
```js run demo
function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Quanto quer adicionar?', 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
# Crie new Accumulator

Create a constructor function `Accumulator(startingValue)`.
Crie uma função construtora `Accumulator(startingValue)`.

Object that it creates should:
O objeto que ela cria deve:

- 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`.
- Armazenar o "valor atual" na propriedade `value`. O valor inicial é definido no argumento do construtor `startingValue` .
- O método `read()` deve usar `prompt` para ler um novo número e adicioná-lo ao `value`.

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
Em outras palavras, a propriedade `value` é a soma de todos os valores digitados pelo usuário com o valor inicial `startingValue`.

Here's the demo of the code:
Aqui está uma demonstração do código:

```js
let accumulator = new Accumulator(1); // initial value 1
let accumulator = new Accumulator(1); // valor inicial 1

accumulator.read(); // adds the user-entered value
accumulator.read(); // adds the user-entered value
accumulator.read(); // adiciona o valor digitado pelo usuário
accumulator.read(); // adiciona o valor digitado pelo usuário

alert(accumulator.value); // shows the sum of these values
alert(accumulator.value); // apresenta a soma destes valores
```

[demo]
Loading