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

JavascriptInternet Explorer

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

Avoid use of arrow functions if you need to support IE 11 as it is not supported

Change those to regular functions and your code should work as you expect

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";
});

Solution 3 - Javascript

In general, before arrow functions were arrow functions, they were regular JS functions. So with IE11 we just have to take a step back in time

var fruits=["apple","banana","orange"];

var modernResult=fruits.find(e => e.includes("nana"));
console.log(modernResult);

var IEresult=fruits.find(function(e){return e.includes("nana")});
console.log(IEresult);

Solution 4 - Javascript

IE doesn't support the arrow notation as of now but there is a handy and fast way for transpiling your ES6 codes to ES5.1 for working in IE. visit the Babel website then paste your codes in the left box and copy the right box code that is transpiled to the earlier version of JavaScript.

For example, your code is transpiled to:

"use strict";

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";
});

Solution 5 - Javascript

Internet Explorer(IE) doesn't support the ES6 functions at all, but you could install BabelJS. But you can also write the vanillaJS functions instead.

For example:

const hasBrownEyes = eye.color === "brown" ? true : false

Or just vanilla.js:

var hasBrownEyes = false; // default answer, it will be overwritten if it has a brown eye

if (eye.color === "brown") {
    return true;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprakashkadakolView Question on Stackoverflow
Solution 1 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavascriptFelipe SabinoView Answer on Stackoverflow
Solution 3 - JavascriptSzél LajosView Answer on Stackoverflow
Solution 4 - JavascriptAmerllicAView Answer on Stackoverflow
Solution 5 - JavascriptValdemar VreemanView Answer on Stackoverflow