【发布时间】:2016-11-02 00:15:07
【问题描述】:
我想通过构建某种模块来以更好的方式构造这段代码。我正在使用 ES6,所以我尝试使用 import / export。
示例函数
function() {
var graph = new joint.dia.Graph;
var paper = new joint.dia.Paper({
el: $('#canvas'),
width: 600,
height: 200,
model: graph,
gridSize: 1
});
var rect = new joint.shapes.basic.Rect({
position: { x: 100, y: 30 },
size: { width: 100, height: 30 },
attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }
});
var rect2 = rect.clone();
rect2.translate(300);
var link = new joint.dia.Link({
source: { id: rect.id },
target: { id: rect2.id }
});
graph.addCells([rect, rect2, link]);
}
所以我想得到这样的东西,这样可读性会更好:
script.js
import * from './config.js';
function() {
var graph = graph();
var paper = paper();
var rect = rect();
var rect2 = rect.clone();
rect2.translate(300);
var link = link();
graph.addCells([rect, rect2, link]);
}
所有内容都应该在另一个文件中:
config.js
export
var graph = new joint.dia.Graph,
var paper = new joint.dia.Paper({
el: $('#canvas'),
width: 600,
height: 200,
model: graph,
gridSize: 1
});
// ...
但是这种尝试没有正确的语法。这应该怎么做?
【问题讨论】:
标签: javascript function ecmascript-6 es6-modules