【问题标题】:Convert ES6 JavaScript to ES5 without transpiler不使用转译器将 ES6 JavaScript 转换为 ES5
【发布时间】:2016-05-23 02:27:44
【问题描述】:

我不熟悉新的 JavaScript ES6 编码约定,并且已经获得了一些代码,我需要它是普通的旧 JavaScript ES5。

我需要在不使用 Babel 或任何其他转译器的情况下转换此 JS 代码。我不能使用 Babel,因为我不允许在工作中使用它。

我意识到所有的“const”都可以转换为“var”,但不确定新的箭头函数和其他项目。

我尝试过转换但得到:

Uncaught ReferenceError: line is not defined

我想转换成 ES5 的 ES6 代码是:

const data = [{ "rec": "1", "region": "LEFT", "intrface": "Line-1" },{ "rec": "1", "region": "LEFT", "intrface": "Line-2" },{ "rec": "1", "region": "RIGHT", "intrface": "Line-3" },{ "rec": "1", "region": "RIGHT", "intrface": "Line-4" }];

const s = Snap("#svg");
const height = 40;
const canvasWidth = 400;
const lineWidth = 180;
const rightOffset = canvasWidth/2 - lineWidth;

const leftLines = data.filter((line) => !isRightLine(line));
const rightLines = data.filter(isRightLine);

leftLines.forEach(drawLine);
rightLines.forEach(drawLine);

const numberOfLines = Math.max(leftLines.length, rightLines.length);
const rectSize = 20;
const rectangles = [];

for (let i = 0; i < numberOfLines; i++) {
    rectangles.push(drawRect(i));
}

function drawLine(data, index) {
    const {intrface} = data;
    const isRight = isRightLine(data);
    const x = isRight ? canvasWidth/2 + rightOffset : 0;
  const y = height * (index + 1);
  const stroke = isRight ? 'red' : 'black';

    const line = s.line(x, y, x + 180, y);
  line.attr({
    stroke,
    strokeWidth: 1
  });

  const text = s.text(x + 10, y - 5, intrface);

  text.attr({
    fill: stroke,
    cursor: 'pointer'
  });

  text.click(() => {
    console.log('clicked', data);

    //window.location.href = "http://stackoverflow.com/";
  });
}

function isRightLine({region}) {
    return region === 'RIGHT';
}

function drawRect(index) {
    const x = canvasWidth/2 - rectSize/2;
  const y = height * (index + 1) - rectSize/2;
    const rectangle = s.rect(x, y, rectSize, rectSize);

  rectangle.attr({
    fill: 'black'
  });

  console.log('rr', x, y);

  return rectangle;
}

【问题讨论】:

  • 如果 let/const 使用正确,单独的转换可能是不够的,你可能会通过提升来重构它。还有为什么不用 babel 呢?
  • 为什么不直接使用 Babel 看看输出呢?他们大部分都做对了。
  • 这两个箭头函数实例可以简单地用函数表达式替换,不会有任何中断。
  • 使用 Babel 的限制是技术限制还是意识形态限制?如果是后者,并且您要求人们为您转换代码,您如何确定他们不只是通过 Babel 运行它?
  • 在家里的代码上运行 Babel。

标签: javascript


【解决方案1】:

我假设由于您对 es6 不是很熟悉,您可能对 babeljs.io 不太熟悉,它为您提供了转换回来的选项:

检查这个link up。

"use strict";

var data = [{ "rec": "1", "region": "LEFT", "intrface": "Line-1" }, { "rec": "1", "region": "LEFT", "intrface": "Line-2" }, { "rec": "1", "region": "RIGHT", "intrface": "Line-3" }, { "rec": "1", "region": "RIGHT", "intrface": "Line-4" }];

var s = Snap("#svg");
var height = 40;
var canvasWidth = 400;
var lineWidth = 180;
var rightOffset = canvasWidth / 2 - lineWidth;

var leftLines = data.filter(function (line) {
  return !isRightLine(line);
});
var rightLines = data.filter(isRightLine);

leftLines.forEach(drawLine);
rightLines.forEach(drawLine);

var numberOfLines = Math.max(leftLines.length, rightLines.length);
var rectSize = 20;
var rectangles = [];

for (var i = 0; i < numberOfLines; i++) {
  rectangles.push(drawRect(i));
}

function drawLine(data, index) {
  var intrface = data.intrface;

  var isRight = isRightLine(data);
  var x = isRight ? canvasWidth / 2 + rightOffset : 0;
  var y = height * (index + 1);
  var stroke = isRight ? 'red' : 'black';

  var line = s.line(x, y, x + 180, y);
  line.attr({
    stroke: stroke,
    strokeWidth: 1
  });

  var text = s.text(x + 10, y - 5, intrface);

  text.attr({
    fill: stroke,
    cursor: 'pointer'
  });

  text.click(function () {
    console.log('clicked', data);

    //window.location.href = "http://stackoverflow.com/";
  });
}

// you might want to change this - howeverever you will have to change everywhere in the code that calls isRightLine to pass a primitive vs an object.
function isRightLine(_ref) {
  var region = _ref.region;

  return region === 'RIGHT';
}

function drawRect(index) {
  var x = canvasWidth / 2 - rectSize / 2;
  var y = height * (index + 1) - rectSize / 2;
  var rectangle = s.rect(x, y, rectSize, rectSize);

  rectangle.attr({
    fill: 'black'
  });

  console.log('rr', x, y);

  return rectangle;
}

【讨论】:

  • 感谢@Neta Meta。将尝试代码,看看我是怎么走的。会让你知道的。
  • 听起来不错。谢谢。它应该可以正常工作,只需将代码转回即可。
  • 感谢您的帮助。正如我在开头提到的,我不知道我可以使用 babeljs.io 从 es6 到 es5 进行反向操作。这是一个我还没有机会调查的领域。您的转换一切正常。感谢所有的反馈和帮助。
猜你喜欢
  • 2014-10-12
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 2017-01-07
  • 2019-09-24
  • 2020-05-29
  • 2016-03-28
相关资源
最近更新 更多