Bird
del desafío anterior:
function Bird() {Observe que el
this.name = "Albert";
this.color = "blue";
this.numLegs = 2;
// "this" inside the constructor always refers to the object being created
}
let blueBird = new Bird();
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; // => AlbertAl igual que cualquier otro objeto, sus propiedades se pueden acceder y modificar:
blueBird.color; // => blue
blueBird.numLegs; // => 2
blueBird.name = 'Elvira';
blueBird.name; // => Elvira
Dog
de la última lección para crear una nueva instancia de Dog
, asignándola a un hound
variable.
hound
debe ser creado usando el constructor de Dog
.
testString: 'assert(hound instanceof Dog, "hound
should be created using the Dog
constructor.");'
- text: Tu código debe usar el new
operador para crear una instance
de Dog
.
testString: 'assert(code.match(/new/g), "Your code should use the new
operator to create an instance
of Dog
.");'
```