4

The only thing that is giving me problems is executing an anonymous function call. I even made an empty call to see if there was problems with the code inside; that isn't the case.

This is the format I write them in:

(function(){})(); 

I'm positive that that is correct and standard use, but it keeps throwing this error:

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

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

the code excerpt above is no different than what's in my program

10
  • Does the specific code posted (empty arguments, empty body) give you an error? If not, what's the simplest code that does give you the error? If it does produce the error, what context are you calling it in, and what Javascript engine is complaining about it? Does a script whose only content is that line give you the error? Commented Jun 7, 2015 at 1:58
  • 1
    The posted code produces no error in Chrome 43. Just tried it. Commented Jun 7, 2015 at 1:59
  • @user2357112 The exact code posted(the empty anonymous function) throws the error. I am using vanilla js with no added libraries and there's only 120 lines of code. I'm calling it in the global scope. Commented Jun 7, 2015 at 1:59
  • 2
    What happens if you remove the other 119 lines? Have you checked that the previous line has a semicolon? Commented Jun 7, 2015 at 1:59
  • 1
    @Lemony-Andrew Function declarations don't have this problem. Function expressions do. Commented Jun 7, 2015 at 2:09

2 Answers 2

8

The code giving you trouble 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();
    })();

Stripping out comments, we get

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

The assignment to ctrl.deleteObject is not terminated by a semicolon, and the parentheses on the next line look like a valid continuation of the assignment, so Javascript doesn't insert a semicolon for you. Instead of an assignment and an anonymous function call, you end up calling the function you were trying to assign to ctrl.deleteObject, and then calling its return value, which isn't a function.

Sign up to request clarification or add additional context in comments.

Comments

4

Maybe you have something like

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

It becomes

(123)();

But 123 is not a function. So it throws

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

To fix it, add a semi-colon:

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

Note the semi-colon is needed in function expressions, but not necessary in function declarations:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.