When you say
foo.print
you will be getting a reference to the function, but the function is actually attached to foo
object which is lost by passing the foo.print
. So, print
becomes an unbound function. You can confirm this like this.
var a = foo.print;
a(); // `this` will be referring to global now
To avoid this, you should bind
the function with the object, like this
element.addEventListener("click", foo.print.bind(foo));
Now we are making sure that the function is bound to the foo
object. You can check this, like this
var a = foo.print.bind(foo);
a(); // a