my first post on stackoverflow!!!
I am not sure why my function() is not working in the if(statment) inside the for loop...
I understand ways around it. That I could just add a var answer and console.log that in the first anonymous function, BUT I want to learn why the function is not working... HELP!
I've been learning bind, call, apply and wanted to create this example to try it and stumbled upon this question.
var fruits = [];
var Fruit = function(name, color, quantity) {
this.name = name;
this.color = color;
this.quantity = quantity;
};
addFruit = function(name, color, quantity) {
fruits.push(new Fruit(name, color, quantity));
}
// filled fruits[] with objects.
addFruit("strawberry", "red", 20);
addFruit("watermelon", "green", 5);
addFruit("orange", "orange", 10);
// trying to console.log "an" instead of "a" because it preceeds "orange"
var greet = function() {
var vowels = "aeiou";
for (var i = 0; i < vowels.length; i++) {
if (this.name.charAt(0) === vowels.charAt(i)) {
// here is where this function does not work...
function() {
console.log("I am an " + this.name);
};
break;
}
else {
function() {console.log("I am a " + this.name)}
}
}
}
greet.call(fruits[2])
Aucun commentaire:
Enregistrer un commentaire