Ghost Together
1 min readDec 5, 2018

--

Oh, it will be. The context (global scope/window) is taken all the way to the last function… if the first function was called from window/global scope.

function a() { alert(window === this); b();  }
function b() { alert(window === this); }
a();

This will output true twice in a row: even inside b() this === window, even though b() was called from a(); It’s a chaining effect.

The alternative to this would be to instantiate b as a constructor. Just add to the above example following lines:

var c = new b();
c();

Now a call to c() returns false. (Even though it’s still calling b())

Think of c as b “version 2”. (its own instance.)

That’s because we instantiated it with new keyword.

When that happens a function owns its this context now. Even if it was called from window scope. So it’s not merely what context it was called from, but also how it was created.

--

--

Ghost Together
Ghost Together

Written by Ghost Together

Ghost Together @ https://semicolon.dev is an alternative to Twitter. Sign up to meet other makers of things.

No responses yet