I have the following code that works exactly as expected (that is to go through the array, showing ONLY each element, in this case "Miguel" and "Celeste"):
var runners = ['Miguel', 'Celeste'];
Array.prototype.winner = function() {
for (var i = 0; i < this.length; i++) {
console.log(runners[i]);
}
}
runners.winner();
My question is why is not this? Why do you always do that last loop and print your own function? Is there any implicit property that does not go into the count ( this.length
)? If yes, then there is no way to do this with for(var i in this) {
right?
var runners = ['Miguel', 'Celeste'];
Array.prototype.winner = function() {
for(var i in this) {
console.log(runners[i]);
}
}
runners.winner();