无需转换器即可将ES6 Javascript转换为ES5

时间:2016-05-23 02:27:45

标签: javascript

我不熟悉新的JavaScript ES6编码约定,并且已经提供了一些代码,我需要将它作为普通的旧版本ES5。

非常感谢人们可以在不使用Babel或任何其他转换器的情况下协助转换此js代码。

不能使用Babel作为不允许在工作中使用。

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

尝试过转换但得到:

未捕获的ReferenceError:未定义行

我想转换为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;
}

1 个答案:

答案 0 :(得分:1)

我会假设因为你对es6不太熟悉,你可能不太熟悉for i in range(len(listRaw)),它可以让你选择转换回来:

请检查此link

babeljs.io