【问题标题】:Organize helper in sails.js在sails.js 中组织助手
【发布时间】:2018-02-02 19:20:50
【问题描述】:

有没有办法在sails.js v1.0 中的帮助程序中构造代码?

目前我有类似的东西:

module.exports = {

  friendlyName: 'Example helper that does three things',

  description: '',

  inputs: {},

  exits: {},

  fn: async function (inputs, exits) {
    const doFirstThing = function () {
      // do something
    };

    const doSecondThing = function () {
      // do something
    };

    const doThirdThing = function () {
      // do something
    };

    doFirstThing();
    doSecondThing();
    doThirdThing();
  },
};

doFirstThingdoSecondThingdoThirdThing这三个函数各有大约15行代码。现在,代码很难阅读。有没有办法将函数放在 fn 函数下方或以任何其他更具可读性的方式构造它?

【问题讨论】:

    标签: javascript sails.js


    【解决方案1】:

    您始终可以在外部定义函数并稍后将其分配给您的module.exports 对象。 doFirstThing和另外两个函数如果不使用doEverything函数的闭包变量也可以单独定义

    async function doEverything(inputs, exits) {
        const doFirstThing = function () {
            // do something
        };
    
        const doSecondThing = function () {
            // do something
        };
    
        const doThirdThing = function () {
            // do something
        };
    
        doFirstThing();
        doSecondThing();
        doThirdThing();
    }
    module.exports = {
    
        friendlyName: 'Example helper that does three things',
    
        description: '',
    
        inputs: {},
    
        exits: {},
    
        fn: doEverything
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-06
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2020-08-16
      • 2014-04-22
      • 1970-01-01
      相关资源
      最近更新 更多