Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this arrow function work in IE 11?

Below piece of code does not work in IE 11, it throws a syntax error in the console

g.selectAll(".mainBars")     .append("text")     .attr("x", d => (d.part == "primary" ? -40 : 40))     .attr("y", d => +6)     .text(d => d.key)     .attr("text-anchor", d => (d.part == "primary" ? "end" : "start")); 

Using d3.js bipartite chart for visualization

This code causing the issue in above statement d=>(d.part=="primary"? -40: 40)

like image 700
prakashkadakol Avatar asked Oct 24 '16 10:10

prakashkadakol


People also ask

Is arrow function supported in IE11?

Arrow functions are not supported in IE11 or earlier.

Do all browsers support arrow functions?

BROWSER SUPPORT FOR JAVASCRIPT Arrow functionsChrome version 4 to 44 doesn't supports. Chrome version 45 to 70 supports JAVASCRIPT Arrow functions.

Why is arrow not hoisted?

An arrow function expression is an anonymous function expression written with the “fat arrow” syntax ( => ). Like traditional function expressions, arrow functions are not hoisted, and so you cannot call them before you declare them. They are also always anonymous—there is no way to name an arrow function.


1 Answers

You're using arrow functions. IE11 doesn't support them. Use function functions instead.

Here's Babel's translation of that to ES5:

g.selectAll(".mainBars").append("text").attr("x", function (d) {   return d.part == "primary" ? -40 : 40; }).attr("y", function (d) {   return +6; }).text(function (d) {   return d.key; }).attr("text-anchor", function (d) {   return d.part == "primary" ? "end" : "start"; }); 

Since none of the code uses this, you don't have to worry about preserving arrow function this behavior (since traditional functions get their this by how they're called, but arrow functions close over this). But if the code did use this and you wanted it to behave like an arrow function would, you'd want to use the usual techniques for that.

like image 120
2 revs Avatar answered Sep 28 '22 00:09

2 revs