2018-10-08 13:34:43 -04:00

2.4 KiB

id, title, localeTitle, challengeType
id title localeTitle challengeType
587d7dad367417b2b2512b78 Use a Constructor to Create Objects Usa un constructor para crear objetos 1

Description

Aquí está el constructor Bird del desafío anterior:
function Bird() {
  this.name = "Albert";
  this.color = "blue";
  this.numLegs = 2;
  // "this" inside the constructor always refers to the object being created
}

let blueBird = new Bird();
Observe que el new operador se utiliza al llamar a un constructor. Esto le dice a JavaScript que cree una nueva instance de Bird llamada blueBird . Sin el new operador, this dentro del constructor no apuntaría al objeto recién creado, dando resultados inesperados. Ahora, blueBird tiene todas las propiedades definidas dentro del constructor Bird :
blueBird.name; // => Albert
blueBird.color; // => blue
blueBird.numLegs; // => 2
Al igual que cualquier otro objeto, sus propiedades se pueden acceder y modificar:
blueBird.name = 'Elvira';
blueBird.name; // => Elvira

Instructions

Usa el constructor Dog de la última lección para crear una nueva instancia de Dog , asignándola a un hound variable.

Tests

tests:
  - text: <code>hound</code> debe ser creado usando el constructor de <code>Dog</code> .
    testString: 'assert(hound instanceof Dog, "<code>hound</code> should be created using the <code>Dog</code> constructor.");'
  - text: Tu código debe usar el <code>new</code> operador para crear una <code>instance</code> de <code>Dog</code> .
    testString: 'assert(code.match(/new/g), "Your code should use the <code>new</code> operator to create an <code>instance</code> of <code>Dog</code>.");'

Challenge Seed

function Dog() {
  this.name = "Rupert";
  this.color = "brown";
  this.numLegs = 4;
}
// Add your code below this line


Solution

function Dog() {
  this.name = "Rupert";
  this.color = "brown";
  this.numLegs = 4;
}
const hound = new Dog();