Bird
del desafío anterior: función Bird () {Observe que el
this.name = "Albert";
this.color = "blue";
this.numLegs = 2;
// "esto" dentro del constructor siempre se refiere al objeto que se está creando
}
Deje que 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 de Bird
: blueBird.name; // => AlbertAl igual que cualquier otro objeto, se puede acceder y modificar sus propiedades:
blueBird.color; // => azul
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
.");'
```