Javascript “Uncaught TypeError: object is not a function” associativity question

前端 未结 6 447
日久生厌
日久生厌 2020-12-02 14:57

Code is as follows:


    hello




        
相关标签:
6条回答
  • 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.

    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.

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

    changed to:

    <input type="button" name="clearEmployerBtn" onClick="clearEmployer();">
    
    0 讨论(0)
  • 2020-12-02 15:34

    Try to have the function body before the function call in your JavaScript file.

    0 讨论(0)
  • 2020-12-02 15:40

    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.

    0 讨论(0)
  • 2020-12-02 15:42

    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.

    0 讨论(0)
  • 2020-12-02 15:44

    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.

    0 讨论(0)
  • 2020-12-02 15:53

    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.

    0 讨论(0)
提交回复
热议问题