What is lexical this in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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);
});
raja
Published on 24-Jan-2018 11:39:57
Advertisements