I created an object with a function that refers to a property of itself using the 'this' keyword.
If I call it normally, it works fine. But if I call pass it as an argument in a forEach function it doesn't work unless I bind it to itself.
Is there a more elegant solution?
Here is an example:
var foobar = {
foo : function (number) {
this.bar();
},
bar : function () {
return "string"
}
};
foobar.foo(0);
[1,2,3].forEach(foobar.foo); //won't work
[1,2,3].forEach(foobar.foo.bind(foobar)); //works
Fiddle: http://jsfiddle.net/q29yatc2/