JS Daily 23 - Function Apply() Method on Objects

Last updated on 13 Dec, 2020

What is the output of following code?

const obj = {
  a : function(){

obj.a.apply({b : 10});

The output is 10.

apply() is a method of function object. Even though a is called in the context of obj, the invocation context is overridden by apply() method. Therefore, when obj.a() is invoked, this has the value of {b : 10} which is set by apply() method. That is why 10 is printed in the console.

--- ○ ---
Joby Joseph
Web Architect