记录一下因为代码不规范带来的错误。
复现:集齐柯里化闭包和立即执行函数,先执行闭包,紧接着写立即执行函数。
先看代码uncurrying方法:返回一个function
Function.prototype.uncurrying = function() {
var _this = this
return function () {
var obj = Array.prototype.shift.call( arguments )
console.log(obj)
return _this.apply( obj, arguments )
}
}
对比两段代码
// 第一段 运行会报错
let push = Array.prototype.push.uncurrying()
(function() {
push(arguments, 4)
})([1,2,3])
// 第二段 正常运行
let push = Array.prototype.push.uncurrying();
(function() {
push(arguments, 4)
})([1,2,3])
对比上方两端代码 .push.uncurrying()
区别只是有没有加分号
第一段就会报错,错误如下所示
at Function.push (<anonymous>)
at \test\omg.js:6:18
at Object.<anonymous> (\test\omg.js:12:1)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
在编译时没有分号,两行被编译成1行,导致了运行错误。
以下就是没加分号后编译效果,显然他把下一行自执行函数当作了闭包函数的参数
let push = Array.prototype.push.uncurrying()(function() { push(arguments, 4) })([1,2,3]);