Skip to content Skip to sidebar Skip to footer

Immediately Invoked Function Expression Throws "object Is Not A Function"

I'm defining various modules in a Javascript file: var module = {/* ... */} (function(){ console.log('Invoked'); })() However the IIFE throws an error: > TypeError: object

Solution 1:

The module definition needs a semicolon at the end of the declaration:

varmodule = {/* ... */}; // <======= Semicolon!

(function(){
    console.log('Invoked');
})()

Without it Javascript is trying to call the object:

varmodule = {/* ... */}(function(){console.log('Invoked');})()

Or shortened:

varmodule = {/* ... */}()

You'd get the same problem when trying to writing two IIFEs next to each other:

(function(){})()
(function(){})()

This doesn't work because a single function declaration returns undefined:

TypeError: undefined is not a function

Post a Comment for "Immediately Invoked Function Expression Throws "object Is Not A Function""