Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting => () => to ES5 [duplicate]

I'm in the process of converting some scripting from from ES6 to ES5 due to a dependency on the system executing the scripts. I'm running into an issue with this particular command:

transition.selectAll('path.hidden-arc')
   .attrTween('d', d => () => middleArcLine(d));

I'm not sure what they're trying to accomplish with the '=> () =>' syntax and am unsure how to convert this to ES5 standard functions. Any help is greatly appreciated.

like image 755
Josh Simmons Avatar asked Aug 05 '20 02:08

Josh Simmons


People also ask

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

Can you use arrow functions in ES5?

The arrow function concept was introduced in ES6. With the help of this, we can write a shorter and concise syntax for a normal function which we used to write in ES5.

What is ES5 vs ES6?

ES5 is an abbreviation of ECMAScript 5 and also known as ECMAScript 2009. The sixth edition of the ECMAScript standard is ES6 or ECMAScript 6. It is also known as ECMAScript 2015. ES6 is a major enhancement in the JavaScript language that allows us to write programs for complex applications.


Video Answer


2 Answers

It's using arrow functions to represent a function that returns a function that returns the value from calling the middleArcline function. In ES5 it could look something like this:

transition.selectAll('path.hidden-arc').attrTween('d', function (d) {
    return function () {
        return middleArcLine(d);
    };
});

Note that Babel is a great tool for compiling between different versions of JavaScript

like image 140
jason_r Avatar answered Oct 17 '22 22:10

jason_r


I guess it's a function calling a function. Try this

transition.selectAll('path.hidden-arc')
   .attrTween('d', function (d) {
                       return function() {
                           return middleArcLine(d)
                       }
                   }
             );

I might be wrong, but just attempting it.

like image 29
Vasanth Gopal Avatar answered Oct 17 '22 23:10

Vasanth Gopal