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

前端 未结 7 1896
不思量自难忘°
不思量自难忘° 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:16

    The error is a result of the missing semicolon on the third line:

    window.Glog = function(msg) {
      console.log(msg);
    }; // <--- Add this semicolon
    
    (function(win) {
      // ...
    })(window);
    

    The ECMAScript specification has specific rules for automatic semicolon insertion, however in this case a semicolon isn't automatically inserted because the parenthesised expression that begins on the next line can be interpreted as an argument list for a function call.

    This means that without that semicolon, the anonymous window.Glog function was being invoked with a function as the msg parameter, followed by (window) which was subsequently attempting to invoke whatever was returned.

    This is how the code was being interpreted:

    window.Glog = function(msg) {
      console.log(msg);
    }(function(win) {
      // ...
    })(window);
    
    0 讨论(0)
  • 2020-12-02 11:19

    Error Case:

    var userListQuery = {
        userId: {
            $in: result
        },
        "isCameraAdded": true
    }
    
    ( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;
    

    Output:

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

    Fix: You are missing a semi-colon (;) to separate the expressions

    userListQuery = {
        userId: {
            $in: result
        },
        "isCameraAdded": true
    }; // Without a semi colon, the error is produced
    
    ( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;
    
    0 讨论(0)
  • 2020-12-02 11:20

    When I create a root class, whose methods I defined using the arrow functions. When inheriting and overwriting the original function I noticed the same issue.

    class C {
      x = () => 1; 
     };
     
    class CC extends C {
      x = (foo) =>  super.x() + foo;
    };
    
    let add = new CC;
    console.log(add.x(4));
    

    this is solved by defining the method of the parent class without arrow functions

    class C {
      x() { 
        return 1; 
      }; 
     };
     
    class CC extends C {
      x = foo =>  super.x() + foo;
    };
    
    let add = new CC;
    console.log(add.x(4));
    
    0 讨论(0)
  • 2020-12-02 11:25

    I have faced this issue when I created a new ES2015 class where the property name was equal to the method name.

    e.g.:

    class Test{
      constructor () {
        this.test = 'test'
      }
    
      test (test) {
        this.test = test
      }
    }
    
    let t = new Test()
    t.test('new Test')
    

    Please note this implementation was in NodeJS 6.10.

    As a workaround (if you do not want to use the boring 'setTest' method name), you could use a prefix for your 'private' properties (like _test).

    Open your Developer Tools in jsfiddle.

    0 讨论(0)
  • 2020-12-02 11:32

    For me it was much more simple but it took me a while to figure it out. We basically had in our .jslib

    some_array.forEach(item => {
        do_stuff(item);
    });
    

    Turns out Unity (emscripten?) just doesn't like that syntax. We replaced it with a good old for-loop and it stoped complaining right away. I really hate it that it doesn't show the line it is complaining about, but anyway, fool me twice shame on me.

    0 讨论(0)
  • 2020-12-02 11:35
      **Error Case:**
    
    var handler = function(parameters) {
      console.log(parameters);
    }
    
    (function() {     //IIFE
     // some code
    })();
    

    Output: TypeError: (intermediate value)(intermediate value) is not a function *How to Fix IT -> because you are missing semi colan(;) to separate expressions;

     **Fixed**
    
    
    var handler = function(parameters) {
      console.log(parameters);
    }; // <--- Add this semicolon(if you miss that semi colan .. 
       //error will occurs )
    
    (function() {     //IIFE
     // some code
    })();
    

    why this error comes?? Reason : specific rules for automatic semicolon insertion which is given ES6 stanards

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