Implicit Binding

Memosha Sinha
1 min readSep 16, 2019

--

This is part of the series this keyword in Javascript

https://medium.com/@msinha2801/this-most-confused-mechanism-in-javascript-b60b3fa3de36

What if call site has a context object also called containing object?

'use strict';function implicitInvocation(){console.log(this.a);}var obj= {
a:2,
implicitInvocation:implicitInvocation
}
obj.implicitInvocation();

call site used obj context to reference the function implicitInvocation() so it can be mentioned that obj “contains” the function reference at the time function is called.
If there is a context object for a function reference, the implicit binding rule states that
“it’s that object should be used for the function fall’s this binding

Only the top/last level of an object property reference chain matters to the call site.

function implicitInvocation() {console.log(this.a);}var obj2 = {a: 10,implicitInvocation: implicitInvocation};var obj1 = {a: 2,obj2: obj2};obj1.obj2.implicitInvocation();//42

What if implicit binding is lost?

function implicitInvocation() {console.log(this.a);}var obj = {a: 2,implicitInvocation: implicitInvocation};var implicitBindingLost = obj.implicitInvocation;// function reference /alias !var a = "global variable";implicitBindingLost();//undefined
//incase of strict mode TypeError: Cannot read property 'a' of undefined

Even if implicitBindingLost appears to be a reference to obj.implicitInvocation its a reference to implicitInvocation itself.
The call site is implicitBindingLost which is plain, undecorated call and thus default binding applies here.

--

--

Memosha Sinha

Software Engineer, Fitness lover, Traveller, wanderlust, optimistic