Javascript“未捕获的类型错误:对象不是函数”关联性问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4026891/
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
Javascript "Uncaught TypeError: object is not a function" associativity question
提问by PolandSpring
Code is as follows:
代码如下:
<body>
<a href="javascript:;" id="test">hello</a>
</body>
<script type="text/javascript">
document.getElementById("test").addEventListener("click", function () {
test()
}, false)
function test() {
var postTypes = new Array('hello', 'there')
(function() { alert('hello there') })()
}
</script>
This will throw an:
这将抛出:
"Uncaught TypeError: object is not a function"
“未捕获的类型错误:对象不是函数”
If I wrap the anonymous function call/invocation in another set of parentheses it will execute the alert, but still give me an error. If I put a semi-colon after the "var postTypes" definition then it will completely fine.
如果我将匿名函数调用/调用包装在另一组括号中,它将执行警报,但仍然给我一个错误。如果我在“var postTypes”定义之后加上一个分号,那就完全没问题了。
I was led to believe that javascript does not require semi-colons, so I'm making a guess that there is some weird associativity rules of function application that I am not fully understanding. Why am I getting this error?
我被认为 javascript 不需要分号,所以我猜测函数应用程序存在一些我不完全理解的奇怪关联规则。为什么我收到这个错误?
采纳答案by kennytm
JavaScript does require semicolons, it's just that the interpreter will insert them for you on line breaks where possible*.
JavaScript 确实需要分号,只是解释器会在可能的情况下在换行符处为您插入它们*。
Unfortunately, the code
不幸的是,代码
var a = new B(args)(stuff)()
does notresult in a syntax error, so no ;
will be inserted. (An example which can run is
并不会导致语法错误,所以没有;
将被插入。(一个可以运行的例子是
var answer = new Function("x", "return x")(function(){return 42;})();
To avoid surprises like this, train yourself to always end a statement with ;
.
为了避免这样的意外,训练自己总是以;
.
**这只是一个经验法则,并不总是正确的。插入规则要复杂得多。这个blog page关于分号插入的博客页面有更多详细信息。
回答by CMS
Your code experiences a case where the Automatic Semicolon Insertion(ASI) process doesn't happen.
您的代码遇到自动分号插入(ASI) 过程不会发生的情况。
You should never rely on ASI. You should use semicolons to properly separate statements:
你永远不应该依赖 ASI。您应该使用分号正确分隔语句:
var postTypes = new Array('hello', 'there'); // <--- Place a semicolon here!!
(function() { alert('hello there') })();
Your code was actually trying to invoke the array object.
您的代码实际上是在尝试调用数组对象。
回答by Naomi
I got a similar error and it took me a while to realize that in my case I named the array variable payInvoices and the function also payInvoices. It confused AngularJs. Once I changed the name to processPayments() it finally worked. Just wanted to share this error and solution as it took me long time to figure this out.
我遇到了类似的错误,我花了一段时间才意识到,在我的情况下,我将数组变量命名为 payInvoices,并且函数也命名为 payInvoices。它混淆了 AngularJs。将名称更改为 processPayments() 后,它终于起作用了。只是想分享这个错误和解决方案,因为我花了很长时间才弄明白。
回答by CMPE
Try to have the function body before the function call in your JavaScript file.
尝试在 JavaScript 文件中的函数调用之前使用函数体。
回答by tbriggs707
I was getting this same error and spent a day and a half trying to find a solution. Naomi's answer lead me to the solution I needed.
我遇到了同样的错误,花了一天半的时间试图找到解决方案。Naomi 的回答让我找到了我需要的解决方案。
My input (type=button) had an attribute name
that was identical to a function name that was being called by the onClick event. Once I changed the attribute name
everything worked.
我的输入 (type=button) 有一个name
与 onClick 事件正在调用的函数名称相同的属性。一旦我更改了属性,name
一切正常。
<input type="button" name="clearEmployer" onClick="clearEmployer();">
changed to:
变成:
<input type="button" name="clearEmployerBtn" onClick="clearEmployer();">
回答by Dzintars
I have this error when compiling and bundling TS with WebPack. It compiles export class AppRouterElement extends connect(store, LitElement){....}
into let Sr = class extends (Object(wr.connect) (fn, vr)) {....}
which seems wrong because of missing comma. When bundling with Rollup, no error.
使用 WebPack 编译和捆绑 TS 时出现此错误。由于缺少逗号,它编译export class AppRouterElement extends connect(store, LitElement){....}
成let Sr = class extends (Object(wr.connect) (fn, vr)) {....}
哪个似乎是错误的。与 Rollup 捆绑时,没有错误。