Serge Seredenko Serge Seredenko -4 years ago 126
Javascript Question

How to bind a function to null?

function f() {
return this.x

f = f.bind(null)
f() // undefined
x = 1
f() // 1

I cannot find any page where it says that binding to
has no effect. Everywhere it is written that
becomes a link to the first argument of
, exceptions are not mentioned. Can somebody provide a link to somewhere describing such behaviour?

Answer Source

Link to the paragraph, where this effect is specified. The problem was, falsey this is replaced with global object not while binding, but at the moment of bound function call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download