当我把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

我做错了什么?


当前回答

我在这种情况下也遇到了同样的问题:

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

实际上,之前({颜色,容量,品牌}=汽车);需要看到分号。

其他回答

该错误是由于第三行缺少分号造成的:

window.Glog = function(msg) {
  console.log(msg);
}; // <--- Add this semicolon

(function(win) {
  // ...
})(window);

ECMAScript规范对自动插入分号有特定的规则,但是在这种情况下,分号不会自动插入,因为下一行开始的括号表达式可以被解释为函数调用的参数列表。

这意味着没有分号,匿名窗口。Glog函数被调用时使用一个函数作为msg参数,后面跟着(window),它随后试图调用返回的任何东西。

下面是代码的解释:

window.Glog = function(msg) {
  console.log(msg);
}(function(win) {
  // ...
})(window);

我在React中也有同样的错误,我花了很长时间才解决这个问题,

原因是我的应用没有包含上下文

去你的索引。JSX(或main。jsx),并检查你的应用程序周围是否有Context。

错误案例:

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;

当我创建根类时,我使用箭头函数定义了根类的方法。当继承和覆盖原来的函数时,我注意到同样的问题。

class C {
  x = () => 1; 
 };
 
class CC extends C {
  x = (foo) =>  super.x() + foo;
};

let add = new CC;
console.log(add.x(4));

这个问题可以通过定义父类的方法而不使用箭头函数来解决

class C {
  x() { 
    return 1; 
  }; 
 };
 
class CC extends C {
  x = foo =>  super.x() + foo;
};

let add = new CC;
console.log(add.x(4));

我的例子:(Angular, PrimeNG)

我的错误: 我的版本:

 "@angular/animations": "^12.2.0",
 "@angular/cdk": "^12.2.0",
 "@angular/common": "^12.2.0",
 "@angular/compiler": "^12.2.0",
 "@angular/core": "^12.2.0",
 "@angular/forms": "^12.2.0",
 "@angular/platform-browser": "^12.2.0",
 "@angular/platform-browser-dynamic": "^12.2.0",
 "@angular/router": "^12.2.0",
 "primeng": "^13.0.0-rc.2",
 "quill": "^1.3.7"

我的解决方案: node_modules / primeng fesm2015 / primeng-editor.mjs

如图所示,更新Quill的导入