I am writing helper function to loop through all array elements - just for learning js.
here is the code:
function arrayLoop(array, func){
var ar = array.length,
i=0;
for ( i = 0; i < ar; i++ ){
func(i);
};
};
it is working when i use it like:
var foo = ['aa','bb','cc'];
arrayLoop(foo, function(i){
alert(foo[i]);
});
but when i try to do it inside object and want to use this context - error occurs:
function test(){
this.foo = ['aa','bb','cc'];
this.bar = ['ff','gg','hh'];
}
test.prototype.find = function(){
arrayLoop(this.foo, function(i){
alert(this.bar[i]) //error- there is no this.bar
};
};
How to pass parent this automatically to arrayLoop function ??