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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42036349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:50:53  来源:igfitidea点击:

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

javascripttypeerror

提问by armnotstrong

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

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

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

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

但是当我尝试在同一个 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)

it complains that there is a TypeError:

它抱怨有一个类型错误:

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

What did I do wrong?

我做错了什么?

回答by Josh Crozier

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);

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.

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

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

这意味着如果没有那个分号,匿名window.Glog函数将使用一个函数作为msg参数被调用,然后(window)它随后试图调用返回的任何内容。

This is how the code was being interpreted:

这是代码的解释方式:

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

回答by Shashwat Gupta

Error Case:

错误案例:

var userListQuery = {
    userId: {
        $in: result
    },
    "isCameraAdded": true
}

( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;

Output:

输出:

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

Fix:You are missing a semi-colon (;) to separate the expressions

修复:您缺少分号 (;) 来分隔表达式

userListQuery = {
    userId: {
        $in: result
    },
    "isCameraAdded": true
}; // Without a semi colon, the error is produced

( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;

回答by Nicholas Pipitone

To make semicolon rules simple

使分号规则变得简单

Every line that begins with a (, [, `, or any operator (/, +, - are the only valid ones), must begin with a semicolon.

(, [, ` 或任何运算符(/、+、- 是唯一有效的运算符)开头的每一行都必须以分号开头。

func()
;[0].concat(myarr).forEach(func)
;(myarr).forEach(func)
;`hello`.forEach(func)
;/hello/.exec(str)
;+0
;-0

This prevents a

这可以防止一个

func()[0].concat(myarr).forEach(func)(myarr).forEach(func)`hello`.forEach(func)/hello/.forEach(func)+0-0

monstrocity.

怪物。

Additional Note

附加说明

To mention what will happen: brackets will index, parentheses will be treated as function parameters. The backtick would transform into a tagged template, and regex or explicitly signed integers will turn into operators. Of course, you can just add a semicolon to the end of every line. It's good to keep mind though when you're quickly prototyping and are dropping your semicolons.

提到会发生什么:括号将索引,括号将被视为函数参数。反引号将转换为带标签的模板,正则表达式或显式签名的整数将转换为运算符。当然,您可以在每一行的末尾添加一个分号。不过,当您快速制作原型并删除分号时,请记住这一点。

Also, adding semicolons to the end of every line won't help you with the following, so keep in mind statements like

此外,在每一行的末尾添加分号不会帮助您进行以下操作,因此请记住以下语句

return // Will automatically insert semicolon, and return undefined.
    (1+2);
i // Adds a semicolon
   ++ // But, if you really intended i++ here, your codebase needs help.

The above case will happen to return/continue/break/++/--. Any linter will catch this with dead-code or ++/-- syntax error (++/-- will never realistically happen).

上述情况会发生在返回/继续/中断/++/--。任何 linter 都会用死代码或 ++/-- 语法错误(++/-- 永远不会发生)来捕获它。

Finally, if you want file concatenation to work, make sure each file ends with a semicolon. If you're using a bundler program (recommended), it should do this automatically.

最后,如果您希望文件串联工作,请确保每个文件都以分号结尾。如果您使用的是打包程序(推荐),它应该会自动执行此操作。

回答by tfrascaroli

For me it was much more simple but it took me a while to figure it out. We basically had in our .jslib

对我来说,这要简​​单得多,但我花了一段时间才弄明白。我们基本上有我们的 .jslib

some_array.forEach(item => {
    do_stuff(item);
});

Turns out Unity (emscripten?) just doesn't like that syntax. We replaced it with a good old for-loop and it stoped complaining right away. I really hate it that it doesn't show the line it is complaining about, but anyway, fool me twice shame on me.

事实证明 Unity (emscripten?) 只是不喜欢这种语法。我们用一个很好的旧 for 循环替换了它,它立即停止了抱怨。我真的很讨厌它没有显示它所抱怨的路线,但无论如何,欺骗我两次对我感到羞耻。

回答by GuyT

I have faced this issue when I created a new ES2015 class where the property name was equal to the method name.

当我创建一个新的 ES2015 类时,我遇到了这个问题,其中属性名称等于方法名称。

e.g.:

例如:

class Test{
  constructor () {
    this.test = 'test'
  }

  test (test) {
    this.test = test
  }
}

let t = new Test()
t.test('new Test')

Please note this implementation was in NodeJS 6.10.

请注意,此实现是在 NodeJS 6.10 中实现的。

As a workaround (if you do not want to use the boring 'setTest' method name), you could use a prefix for your 'private' properties (like _test).

作为一种解决方法(如果您不想使用无聊的“setTest”方法名称),您可以为“私有”属性使用前缀(如_test)。

Open your Developer Tools in jsfiddle.

jsfiddle 中打开您的开发人员工具。

回答by Shashwat Gupta

  **Error Case:**

var handler = function(parameters) {
  console.log(parameters);
}

(function() {     //IIFE
 // some code
})();

Output: TypeError: (intermediate value)(intermediate value) is not a function *How to Fix IT -> because you are missing semi colan(;) to separate expressions;

输出:TypeError: (intermediate value)(intermediate value) is not a function *How to Fix IT -> 因为你缺少 semi colan(;) 来分隔表达式;

 **Fixed**


var handler = function(parameters) {
  console.log(parameters);
}; // <--- Add this semicolon(if you miss that semi colan .. 
   //error will occurs )

(function() {     //IIFE
 // some code
})();

why this error comes?? Reason : specific rules for automatic semicolon insertion which is given ES6 stanards

为什么会出现这个错误??原因: ES6 标准中给出的自动分号插入的特定规则