1.

What are the different way to access “this” inside an inner function for below code?

Answer»

The OUTPUT logged will be:

undefined Johnny Deep

The first console.log prints undefined because heroIdentity()is been INVOKED in the GLOBAL context (i.e., the WINDOW object) where the _name property doesn’t exists.

The way to fix it is by binding, it to hero object by USING bind.

var heroIdentity = hero.getIdentity.bind(hero);


Discussion

No Comment Found