未捕获的TypeError:(中间值)(...)不是函数 [英] Uncaught TypeError: (intermediate value)(...) is not a function

查看:75
本文介绍了未捕获的TypeError:(中间值)(...)不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将闭包中的js逻辑作为单个js文件编写时,一切工作正常,

Everything works fine when I wrote the js logic in a closure as a single js file, as:

(function(win){
   //main logic here
   win.expose1 = ....
   win.expose2 = ....
})(window)

但是当我尝试在同一个js文件中的闭包之前插入替代日志记录功能时,

but when I try to insert a logging alternative function before that closure in the same js file,

 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:

it complains that there is a TypeError:

Uncaught TypeError: (intermediate value)(...) is not a function

我做错了什么?

推荐答案

该错误是由于第三行缺少分号引起的:

The error is a result of the missing semicolon on the third line:

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

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

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

The ECMAScript specification has specific rules for automatic semicolon insertion, however in this case a semicolon isn't automatically inserted because the parenthesised expression that begins on the next line can be interpreted as an argument list for a function call.

这意味着,如果没有该分号,则将匿名window.Glog函数作为函数调用,并将其作为msg参数,随后是(window),该函数随后试图调用返回的任何内容.

This means that without that semicolon, the anonymous window.Glog function was being invoked with a function as the msg parameter, followed by (window) which was subsequently attempting to invoke whatever was returned.

这是解释代码的方式:

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

这篇关于未捕获的TypeError:(中间值)(...)不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆