微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将函数声明重构为局部作用域

如何解决将函数声明重构为局部作用域

有没有办法用任何 IDE 或任何优化编译器(GUI、在线、命令行工具,等等)重构 JavaScript 函数声明,以便它更接近它的应用位置,无需内联它?结果是这样的:

  1. 原始文件.js
function outer() {
  console.log('foo');
  inner();
}

function inner() {
  console.log('bar');
}


function other() {
  console.log('baz');
}

module.exports.other = other;
  1. 带有内联(不需要的)的 file.js
function outer() {
  console.log('foo');
  console.log('bar'); // loss of abstraction,especially if the function block is too big
}

module.exports.other = () => {
  console.log('baz');
};
  1. 带有局部作用域函数声明的file.js(良好)
function outer() {
  function inner() {      // we keep the function declaration.. 
    console.log('bar');
  }
  console.log('foo');
  inner();                // ..with it's function call.
}

function other() {
  console.log('baz');
}

module.exports.other = other;

我正在寻找自动执行此操作的方法。我的首要任务是重构内外二重奏而不是 module.exports.other 。提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。