首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ES6 JavaScript转换为不带转换程序的ES5

将ES6 JavaScript转换为不带转换程序的ES5
EN

Stack Overflow用户
提问于 2016-05-23 02:27:44
回答 1查看 39.1K关注 0票数 8

我不熟悉新的JavaScript ES6编码约定,我已经得到了一些代码,我需要这些代码才能成为普通的旧JavaScript ES5。

我需要转换这个JS代码,而不需要使用Babel或任何其他转换程序。我不能使用Babel,因为我不能在工作中使用它。

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

我试过转换,但得到了:

未定义的ReferenceError:行未定义

我希望转换为ES5的ES5代码是:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-23 02:38:37

我假设,由于您对es6不太熟悉,所以您可能不太熟悉babeljs.io,这使您可以选择返回:

检查一下这个 !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; }">链接

代码语言:javascript
复制
"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;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37381794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档