Skip to content
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

Constructor, operator "new" #273

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Yes, it's possible.
Si, es posible.

If a function returns an object then `new` returns it instead of `this`.
Si una función devuelve un objeto, entonces `new` lo devuelve en vez de `this`.

So they can, for instance, return the same externally defined object `obj`:
Por lo tanto pueden, por ejemplo, devolver el mismo objeto definido externamente `obj`:

```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
# Dos funcionesun objeto

Is it possible to create functions `A` and `B` such as `new A()==new B()`?
Es posible crear las funciones `A` y `B` como `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.
Si es posible, entonces proporcione un ejemplo de su código.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describe("calculator", function() {
calculator.read();
});

it("when 2 and 3 are entered, the sum is 5", function() {
it("cuando se ingresa 2 y 3, la suma es 5", function() {
assert.equal(calculator.sum(), 5);
});

it("when 2 and 3 are entered, the product is 6", function() {
it("cuandose ingresa 2 y 3, el producto es 6", function() {
EzequielCaste marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(calculator.mul(), 6);
});

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
# Crear nueva Calculadora

Create a constructor function `Calculator` that creates objects with 3 methods:
Crear una función constructora `Calculator` que crea objetos con 3 métodos:

- `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.
- `read()` pide dos valores usando `prompt` y los recuerda en las propiedades del objeto.
- `sum()` devuelve la suma de estas propiedades.
- `mul()` devuelve el producto de multiplicación de estas propiedades.

For instance:
Por ejemplo:

```js
let calculator = new Calculator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ describe("Accumulator", function() {
prompt.restore();
});

it("initial value is the argument of the constructor", function() {
it("valor inicial es el argumento del constructor", function() {
let accumulator = new Accumulator(1);

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

it("after reading 0, the value is 1", function() {
it("después de leer 0, el valor es 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("después de leer 1, el valor es 2", function() {
let accumulator = new Accumulator(1);
prompt.returns("1");
accumulator.read();
assert.equal(accumulator.value, 2);
});
});
});
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('Cuánto más agregar?', 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
# Crear nuevo Acumulador

Create a constructor function `Accumulator(startingValue)`.
Crear una función contructor `Accumulator(valorInicial)`.
EzequielCaste marked this conversation as resolved.
Show resolved Hide resolved

Object that it creates should:
El objeto que crea debería:

- 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`.
- Almacene el "valor actual" en la propiedad `value`. El valor inicial se establece en el argumento del constructor `valorInicial`.
EzequielCaste marked this conversation as resolved.
Show resolved Hide resolved
- El método `read()` debe usar `prompt` para leer un nuevo número y agregarlo a` value`.
EzequielCaste marked this conversation as resolved.
Show resolved Hide resolved

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
En otras palabras, la propiedad `value` es la suma de todos los valores ingresados por el usuario con el valor inicial `startingValue`.

Here's the demo of the code:
Aquí está la demostración del 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(); // agrega el valor introducido por el usuario
accumulator.read(); // agrega el valor introducido por el usuario

alert(accumulator.value); // shows the sum of these values
alert(accumulator.value); // muestra la suma de estos valores
```

[demo]
Loading