不理解以前的打包编译为什么要这么设计?
不理解以前的打包编译为什么要这么设计这是一段webpack1打包后的代码:
var _index = __webpack_require__(3);
// ...
var render = (0, _index.compile)((0, _index4.getOuterHTML)(el));我知道(a, b, c)这个语句会返回最后一个元素但是我不明白为何要采用(0, _index.function)()的形式去调用引入的函数而不是直接_index.function()或许是有什么历史原因吗?
回复
1个回答
test
2024-06-19
(0, _index.function)() 相当于:
const func = _index.function
func()直接 _index.function() 调用时,function 中的 this 为 _index。而通过 (0, _index.function)() 间接调用,相当于在全局作用域调用函数。在严格模式中,此时函数 function 的 this 为 undefined。
const obj = {
a: 2,
func() {
console.log(this, this?.a)
}
}
obj.func(); // { a: 2 } 2
(0, obj.func)() // undefined undefined回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容