JS:将功能拆分为各种模块部分

JS: Split function into kind of module parts

我想通过构建某种模块以更好的方式构建此代码。我正在使用 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
});
// ...

但是这种尝试没有正确的语法。应该怎么做?

您必须为导出的方法和导入的模块命名。

试试这个代码。

Script.js

import * as config from './config.js';

function() {
    var graph = config.graph();
    var paper = config.paper();
    var rect = config.rect();
    var rect2 = config.rect.clone();
    rect2 = rect2.translate(300);
    var link = config.link();

    config.graph.addCells([rect, rect2, link]);
}

Config.js

export {graph as graph, paper as paper}
var graph = new joint.dia.Graph;
var paper = new joint.dia.Paper({
    el: $('#canvas'),
    width: 600,
    height: 200,
    model: graph,
    gridSize: 1
});
// ...

But this attempt has not the correct syntax.

您正在寻找

export const graph = new joint.dia.Graph;
export const paper = new joint.dia.Paper({
    el: $('#canvas'),
    width: 600,
    height: 200,
    model: graph,
    gridSize: 1
});
…

但是,您需要将它们包装在函数中,以便导入模块不会执行任何副作用并且您可以重复使用它们(在调用您创建的函数时多次调用它们)。你会寻找

export function graph() {
    return new joint.dia.Graph;
}
export function paper(graph) {
    return new joint.dia.Paper({
        el: $('#canvas'),
        width: 600,
        height: 200,
        model: graph,
        gridSize: 1
    });
}
export function rect() {
    return 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' } }
    });
}
export function link(rect1, rect2) {
    return new joint.dia.Link({
        source: { id: rect.id },
        target: { id: rect2.id }
    });
}

以便您可以使用

import {graph, paper, rect, link} from "…"

export default function() {
    const g = graph(),
          p = paper(g),
          r1 = rect(),
          r2 = r.clone();
    r2.translate(300);

    g.addCells([r1, r2, link(r1, r2)]);
}

但是,我不认为您的代码真正受益于分布在多个模块中。只要你不能重用这些函数中的任何一个,你就应该内联它们,它们非常具体。