为什么这个箭头函数在 IE 11 中不起作用? [英] Why doesn't this arrow function work in IE 11?

查看:64
本文介绍了为什么这个箭头函数在 IE 11 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在 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"));

使用d3.js二部图进行可视化

此代码导致上述语句中的问题 d=>(d.part=="primary"? -40: 40)

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

推荐答案

您正在使用箭头函数.IE11 不支持它们.改用 function 函数.

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

这里是 Babel 将其翻译成 ES5:

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

因为没有代码使用this,所以你不必担心保留箭头函数this的行为(因为传统函数得到它们的thiscode> 按它们的调用方式,但箭头函数 关闭 这个).但是,如果代码确实使用了 this 并且您希望它的行为类似于箭头函数,那么您需要使用 常用技术.

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.

这篇关于为什么这个箭头函数在 IE 11 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆