首页 > 解决方案 > 为什么 webpack tapable 使用 new Function('xx', 'some string code') 生成函数而不是直接写函数?

问题描述

我不明白这样做有什么好处。

我自己试了一下,发现新函数其实比直接执行代码要慢很多。

case "sync":
fn = new Function(
                    this.args(),
                    '"use strict";\n' +
                        this.header() +
                        this.content({
                            onError: err => `throw ${err};\n`,
                            onResult: result => `return ${result};\n`,
                            onDone: () => "",
                            rethrowIfPossible: true
                        })
                );
                break;
为什么不这样做

function async() {
  "use strict";
  some code......;
  onError: err => `throw ${err};\n`,
                            onResult: result => `return ${result};\n`,
                            onDone: () => "",
                            rethrowIfPossible: true
}

标签: javascriptwebpack

解决方案


推荐阅读