【问题标题】:JS: Split function into kind of module partsJS:将函数拆分为模块部分
【发布时间】: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


    【解决方案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
    });
    // ...
    

    【讨论】:

      【解决方案2】:

      但是这个尝试没有正确的语法。

      你正在寻找

      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)]);
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 2017-01-29
        • 2022-01-06
        相关资源
        最近更新 更多