Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript";未捕获类型错误:对象不是函数;结合性问题_Javascript - Fatal编程技术网

Javascript";未捕获类型错误:对象不是函数;结合性问题

Javascript";未捕获类型错误:对象不是函数;结合性问题,javascript,Javascript,代码如下: <body> <a href="javascript:;" id="test">hello</a> </body> <script type="text/javascript"> document.getElementById("test").addEventListener("click", function () { test() }, false) function tes

代码如下:

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

document.getElementById(“测试”).addEventListener(“单击”,函数)(){
测试()
},错)
功能测试(){
var postTypes=新数组('hello','there')
(函数(){alert('hello there')})()
}
这将抛出一个:

“未捕获的TypeError:对象不是函数”

如果我将匿名函数调用/调用包装在另一组括号中,它将执行警报,但仍然会给我一个错误。如果我在“var postTypes”定义后加上分号,那么它就完全可以了


我相信javascript不需要分号,所以我猜测函数应用程序中有一些奇怪的关联性规则,我还没有完全理解。为什么会出现这个错误?

JavaScript确实需要分号,只是解释器会尽可能在换行符处为您插入分号*

不幸的是,代码

var a = new B(args)(stuff)()
不会导致语法错误,因此没有
将被插入。(可以运行的示例是

var answer = new Function("x", "return x")(function(){return 42;})();
为了避免这样的意外,请训练自己始终以
结束语句



*这只是一条经验法则,并不总是正确的。插入规则要复杂得多。关于分号插入的这一点有更多的细节。

您的代码遇到了(ASI)过程无法发生的情况

永远不要依赖ASI。应使用分号正确分隔语句:

var postTypes = new Array('hello', 'there'); // <--- Place a semicolon here!!

(function() { alert('hello there') })();

var postTypes=new Array('hello','there');//我遇到了一个类似的错误,我花了一段时间才意识到,在我的例子中,我将数组变量payInvoices命名为payInvoices,函数也将payInvoices命名为payInvoices。这让AngularJs感到困惑。一旦我将名称改为processPayments()它终于起作用了。我只是想分享这个错误和解决方案,因为我花了很长时间才弄明白。请尝试在JavaScript文件中调用函数之前使用函数体。

我遇到了同样的错误,花了一天半的时间试图找到解决方案。Naomi的回答让我找到了我需要的解决方案

我的输入(type=button)有一个属性
name
,该属性与onClick事件调用的函数名相同。一旦我更改了属性
name
,一切正常

<input type="button" name="clearEmployer" onClick="clearEmployer();">

改为:

<input type="button" name="clearEmployerBtn" onClick="clearEmployer();">


我在编译TS并将其与WebPack绑定时出现此错误。它将
导出类AppRouterElement extends connect(store,LitElement){……}
让Sr=class extends(Object(wr.connect)(fn,vr)){……}
这似乎是错误的,因为缺少逗号。在绑定Rollup时,没有错误。

在React中遇到此问题:我尝试在默认导出时分解并使用命名导出,例如:

//module.js
常量myFunction=()=>null
导出默认myFunction
//component.js
//这是错误的:
//从“./module”导入{myFunction}
//应该是这样的:
从“./module”导入myFunction

看起来就像您试图同时创建匿名函数和静态函数,并期望它作为一个函数执行。如果删除函数()可以解决问题,但我无法支持Crockford club,会发生什么情况。@pst:让我换一句话,我根本不在Crock's club:)在这种情况下,我为这些苛刻的话道歉:(我确实很喜欢这个测试。ASI确实让人非常困惑于讨厌的代码!或者:为了避免这样的意外,训练自己编写清晰易读的代码(应该始终适用),并了解ASI的一般规则……这与“知道”没有什么不同JS中的闭包是如何工作的。这里也一样,我有一个名为
alert
的变量,试图调用javascript alert函数,它会说“alert不是函数”。它试图调用
alert
变量,而不是实际的函数