How to use arrow functions used as methods in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Fat arrow function as the name suggests helps in decreasing line of code. The syntax => shows fat arrow. This also avoids you to write the keyword “function” repeatedly. Arrow functions are generally used for a non-method function. Let’s see how to use arrow functions used as methods:

Example

You can try to run the following code to implement arrow functions used as methods

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         'use strict';
         var ob1 = {
            val1: 75,
            val2: 100,
            x: () => document.write(this.val1, this),

            y: function() {
               document.write("<br>"+this.val1, this);
            },

            z: function() {
               document.write("<br>"+this.val2, this);
            },
         }
         ob1.x();
         ob1.y();
         ob1.z();
      </script>
   </body>
</html>
raja
Updated on 07-Jan-2020 08:03:40

Advertisements