Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript "Uncaught TypeError: object is not a function" associativity question

Tags:

javascript

The 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 semicolon after the "var postTypes" definition then it will be completely fine.

I was led to believe that JavaScript does not require semicolons, 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?

like image 295
PolandSpring Avatar asked Oct 26 '10 18:10

PolandSpring


People also ask

Is not a function TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.


2 Answers

JavaScript does require semicolons. It's just that the interpreter will insert them for you on line breaks where possible*.

Unfortunately, the code

var a = new B(args)(stuff)() 

does not result 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 ;.


* This is just a rule of thumb and not always true. The insertion rule is much more complicated. This blog page about semicolon insertion has more detail.

like image 78
kennytm Avatar answered Oct 09 '22 00:10

kennytm


Your code experiences a case where the automatic semicolon insertion (ASI) process doesn't happen.

You should never rely on ASI. You should use semicolons to properly separate statements:

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

Your code was actually trying to invoke the array object.

like image 41
Christian C. Salvadó Avatar answered Oct 09 '22 01:10

Christian C. Salvadó