Estoy haciendo un ejercicio de constructor simple y necesito cambiar un valor después de la creación inicial del constructor. Cuando lo ejecuto, el gato todavía no hace ruido después de que cambié su valor a verdadero.
La función constructora tomará dos parámetros, raining y noise . Estos se pasarán a las claves como su valor.
Cree una tercera clave que será una función llamada makeNoise() . La función comprueba si el valor de la clave de raining es true . Si es así, registrará el valor del noise de la tecla en la consola.
Prima
raining para el objeto cat después de haberlo creado? function Animal(raining, noise) { this.raining = raining, this.noise = noise; this.makeNoise = function(){ if(this.raining) console.log(noise) } } // Creates `dog` and `cat` objects with `raining` and `noise` properties let dog = new Animal(true, 'Woof!'); let cat = new Animal(false, 'Meow!'); // Calls the `makeNoise()` methods on the `dog` and `cat` objects dog.makeNoise(); cat.makeNoise(); // BONUS CODE HERE cat.raining= true;Debe llamar a makeNoise() después de establecer la propiedad raining en true . Cuando llama a makeNoise() antes, la propiedad raining es false (ya que JS se ejecuta (principalmente) secuencialmente), por lo que no se registra nada.
function Animal(raining, noise) { this.raining = raining, this.noise = noise; this.makeNoise = function() { if (this.raining) console.log(noise) } } // Creates `dog` and `cat` objects with `raining` and `noise` properties let dog = new Animal(true, 'Woof!'); let cat = new Animal(false, 'Meow!'); // Calls the `makeNoise()` methods on the `dog` and `cat` objects dog.makeNoise(); cat.makeNoise(); // BONUS CODE HERE cat.raining = true; cat.makeNoise();