简体   繁体   English

未捕获的TypeError:对象不是“((function($))”上的函数

[英]Uncaught TypeError: object is not a function on “(function($)”

Why do I get the following error... 为什么会出现以下错误...

Uncaught TypeError: object is not a function

...at the following line, of a certain JS script? ...在特定的JS脚本的下一行?

(function($){

And why do I get that error only when JS are concatenated? 为什么仅在JS串联时才出现该错误? (I'm using Gulp) (我正在使用Gulp)

And why does it work if I add ; 如果我加了为什么它起作用; before that line, like that: 在那行之前,像这样:

;(function($){

?

update 更新

The preceding line - that is, the object which is not a function, according to the runtime error - on the concatened script was a } , as in: 上一行-根据运行时错误,不是函数的对象-串联脚本上的是} ,如下所示:

storage = {
   //...
}

I'm used to always put semicolon, but not after curly braces. 我习惯总是放分号,但不放在花括号后。

Turns out the curly braces could delimit the end of a statement, like in this case, and then it's recommended to use the semicolon to avoid this error. 事实证明,在这种情况下,花括号可以分隔语句的结尾,然后建议使用分号来避免此错误。 Here 'sa good explanation. 是一个很好的解释。

Javascript ignore missing semi-colon and try to interpret it. JavaScript会忽略缺少的分号,然后尝试对其进行解释。 So if you don't input the semi-colon, it use the next line to see if it should end the line or chain it. 因此,如果您不输入分号,它将使用下一行来查看它是否应该结束该行或将其链接起来。

That allow you to use thing like this : 那可以让你使用这样的事情:

String
.split();

and it will be interpreted like that : 它将被解释为:

String.split();

But , this would also work : 但是 ,这也可以:

String
.split
();

Now, If you have something like this : 现在,如果您有这样的事情:

var a = 'a';
var b = a
(function(){})

JavaScript has no way to know what you really want to do, so it will interpret it like that : JavaScript无法知道您真正想要做什么,因此它将像这样解释它:

var a = 'a';
var b = a(function(){});

Giving you the error [place object type here] is not a function 给你错误[place object type here] is not a function

Bottom line, always put your semi-colon. 最重要的是,请始终使用分号。


Edit 编辑

After seeing your code, here how it is interpreted : 看到您的代码后,这里是如何解释的:

storage = {/**/}(function($){})(jQuery);

So Object ( {} === Object ) is not a function 所以Object{} === Object )不是一个function

When concatenated it believes you're trying to call whatever precedes the (function($) {...} . 串联后,它认为您正在尝试调用(function($) {...}之前的任何内容。

If you put () after a reference it tries to call whatever the reference is. 如果将()放在引用之后,它将尝试调用该引用。 This is why you'll see a lot of JavaScript libraries precede their code with a lone ; 这就是为什么您会看到很多JavaScript库在其代码之前加一个lone的原因;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM