当我把js逻辑写在闭包中作为一个单独的js文件时,一切都很好,如下:
(function(win){
//main logic here
win.expose1 = ....
win.expose2 = ....
})(window)
但是当我尝试在同一个js文件中的闭包之前插入一个日志替代函数时,
window.Glog = function(msg){
console.log(msg)
}
// this was added before the main closure.
(function(win){
//the former closure that contains the main javascript logic;
})(window)
它抱怨有一个TypeError:
Uncaught TypeError: (intermediate value)(...) is not a function
我做错了什么?
错误案例:
var userListQuery = {
userId: {
$in: result
},
"isCameraAdded": true
}
( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;
输出:
TypeError: (intermediate value)(intermediate value) is not a function
修正:你缺少一个分号(;)分隔表达式
userListQuery = {
userId: {
$in: result
},
"isCameraAdded": true
}; // Without a semi colon, the error is produced
( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;
**Error Case:**
var handler = function(parameters) {
console.log(parameters);
}
(function() { //IIFE
// some code
})();
输出:TypeError:(中间值)(中间值)不是函数
*如何修复IT ->,因为你缺少半冒号(;)分开的表达式;
**Fixed**
var handler = function(parameters) {
console.log(parameters);
}; // <--- Add this semicolon(if you miss that semi colan ..
//error will occurs )
(function() { //IIFE
// some code
})();
为什么会出现这个错误?
原因:
ES6标准中给出的自动分号插入的特定规则
我在这种情况下也遇到了同样的问题:
let brand, capacity, color;
let car = {
brand: 'benz',
capacity: 80,
color: 'yellow',
}
({ color, capacity, brand } = car);
而只是一个;在汽车声明结束时,错误消失:
let car = {
brand: 'benz',
capacity: 80,
color: 'yellow',
}; // <-------------- here a semicolon is needed
实际上,之前({颜色,容量,品牌}=汽车);需要看到分号。