【发布时间】:2019-12-13 20:28:51
【问题描述】:
在探索(为了好玩,手动反编译)一些与 Webpack 捆绑并拆分为多个文件的 JavaScript 代码时,我发现一个模块做了一件奇怪的事情:它创建一个函数,然后导出一个调用它的函数的函数创建,而不是仅仅导出第一个函数。
代码的简化、去混淆和反编译版本:
/*
__d is the function used for registering modules
It takes in three arguments:
- a callback, the module's factory
- the module's ID
- the dependency map, an array of the IDs of the module's dependencies (which
is passed in as the last argument of the factory)
*/
__d(
function(
window,
require,
import_default,
import_all,
module,
exports,
dependency_map
) {
// Both are regular functions (not arrow functions)
function example(arg1, arg2) {
// ...
// (note that the function does not use `this`)
// (although it does import another module via `require`)
return result;
}
// Why not just `exports.run_example = example`?
exports.run_example = function(arg1, arg2) {
return example(arg1, arg2);
}
},
// And the other two arguments passed into __d (removed because unimportant)
)
这是为什么?不只是导出第一个函数在功能上是等效的吗?
【问题讨论】:
标签: javascript webpack module bundling-and-minification code-splitting