Uncaught TypeError: (intermediate value)(…) is not a function

前端 未结 7 1897
不思量自难忘°
不思量自难忘° 2020-12-02 10:42

Everything works fine when I wrote the js logic in a closure as a single js file, as:

(function(win){
   //main logic here
   win.expose1 = ....
   win.expos         


        
相关标签:
7条回答
  • 2020-12-02 11:37

    To make semicolon rules simple

    Every line that begins with a (, [, `, or any operator (/, +, - are the only valid ones), must begin with a semicolon.

    func()
    ;[0].concat(myarr).forEach(func)
    ;(myarr).forEach(func)
    ;`hello`.forEach(func)
    ;/hello/.exec(str)
    ;+0
    ;-0
    

    This prevents a

    func()[0].concat(myarr).forEach(func)(myarr).forEach(func)`hello`.forEach(func)/hello/.forEach(func)+0-0
    

    monstrocity.

    Additional Note

    To mention what will happen: brackets will index, parentheses will be treated as function parameters. The backtick would transform into a tagged template, and regex or explicitly signed integers will turn into operators. Of course, you can just add a semicolon to the end of every line. It's good to keep mind though when you're quickly prototyping and are dropping your semicolons.

    Also, adding semicolons to the end of every line won't help you with the following, so keep in mind statements like

    return // Will automatically insert semicolon, and return undefined.
        (1+2);
    i // Adds a semicolon
       ++ // But, if you really intended i++ here, your codebase needs help.
    

    The above case will happen to return/continue/break/++/--. Any linter will catch this with dead-code or ++/-- syntax error (++/-- will never realistically happen).

    Finally, if you want file concatenation to work, make sure each file ends with a semicolon. If you're using a bundler program (recommended), it should do this automatically.

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