Update: I know there are existing implementations of forEach method. The purpose of doing this is to learn and improve my Javascript skills.
I am trying to implement a forEach method for array objects. What I am doing is the following:
var list = ['one', 'two', 'three'];
function forEach(callback){
for(var n = 0; n < this.length; n++){
callback.call(this[n], n);
}
};
list.forEach(function(index){
console.log(index);
console.log(this);
}
);
I am not very good with javascript and I am trying to get better so I've been reading a little bit and I now know that if I do this kind of thing, the context of the "forEach" function would be the object which is calling it, in this case "list".
When this code runs I get the error: "Uncaught TypeError: Object one,two,three has no method 'forEach'".
What is it I am not understanding?
Thanks!
forEachwith theforEachproperty of yourlistobject.