What is lexical this in JavaScript?


Fat arrow function solves the issue of lexical binding “this”. It gets the context of “this “and you can fulfill the same purpose since fast arrow does not have its own this. Fat arrow function as the name suggests helps in decreasing line of code. The syntax => shows fat arrow.

Example

$('.button1').click(function () {
   setTimeout(function () {
      $(this).text('demo');
   } ,400);
});

The above gives an error since the function() defines this as a global object. Let’s see how to solve it using fat arrow function and the context of “this” −

$('.button1').click(function () {
   setTimeout( () => {
      $(this).text(‘demo’) }
  ,400);
});

Updated on: 16-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements