I have a function foo
that calls function bar
. foo
is bound to an element test
.
When being called, bar
stores its caller
function (foo
) inside the set s
. When I now run the function foo
inside s
, strangely this
is now set to Window
. But I did bind the function, so what am I getting wrong?
var s = new Set();
function bar() {
s.add(bar.caller)
}
var test = document.getElementById('test');
test.foo = (function() {
bar();
console.log(this);
}).bind(test);
test.foo(); // output: test div
s.forEach(fn => fn()); // output: Window object
<div id="test"></div>