Anonymous function causing problems

The only thing that is causing me problems is the anonymous function call. I even made an empty call to see if there was any problem with the code inside; this is not true.

This is the format I write in them:

(function(){})(); 

      

I'm sure this is correct and standard usage, but it keeps throwing this error:

Uncaught TypeError: (intermediate) (intermediate) (...) is not a function (anonymous function)

The error can be found HERE while the site is running.

excerpt from the code above is no different from what is in my program

+3


source to share


2 answers


The code causing the problem is

    ctrl.deleteObject = function(obj){
        var index = ctrl.objects.indexOf(obj);
        if( index > -1 ){
            this.objects.splice(index, 1);
        }
    }
    //}


    // //START GAME
     (function(){
     //ctrl.createObject(new PlayerPaddle(50, 50));
        //ctrl.init();
    })();

      

By removing comments, we get



    ctrl.deleteObject = function(obj){
        var index = ctrl.objects.indexOf(obj);
        if( index > -1 ){
            this.objects.splice(index, 1);
        }
    }
     (function(){
    })();

      

The assignment ctrl.deleteObject

does not end with a semicolon, and the parentheses on the next line look like a valid continuation of the assignment, so Javascript doesn't insert the semicolon for you. Instead of assigning and calling a function anonymously, you call the function you are trying to assign ctrl.deleteObject

and then call its return value, which is not a function.

+8


source


Perhaps you have something like

(function() { return 123; })
(function(){})();

      

He becomes

(123)();

      

But 123

it is not a function. So he throws



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

To fix this, add a semicolon:

(function() { return 123; }); // <-- semi-colon
(function(){})(); // No error

      

Note that semi-columns are required in function expressions, but not necessarily in function declarations:

function foo() {} // No semi-colon
(function(){})(); // No error

      

+4


source







All Articles