Default Binding for “this”

Memosha Sinha
1 min readSep 16, 2019

--

Common case of function invocation: standalone function invocation.

function defaultInvocation(){
console.log("Default Invocation",this.a);
}
var a = 2;defaultInvocation();//2 incase of non strict mode

this.a resolves to our global variable a because default binding for “this” applies to function call. defaultInvocation() is called with plain, undecorated function reference and hence default binding is applied.

'use strict';
function defaultInvocation(){
console.log("Default Invocation",this.a);
}
var a = 2;defaultInvocation();
//TypeError: Cannot read property 'a' of undefined

If strict mode is in effect, the global object is not eligible for the default binding and this is set to undefined and hence accessing a using this gives TypeError: Cannot read property ‘a’ of undefined.

If no other rule apply, default rule is applied.

--

--

Memosha Sinha

Software Engineer, Fitness lover, Traveller, wanderlust, optimistic