user3277912
user3277912

Reputation: 201

self execute function is not a function error

var module = (function () {
    console.log('hello world');
}());

the above function did work upon loading, means it did self executed. but when I want to run it for the second time, I wrote module(), it doesn't work, why? I don't want to paste the entire function body to run it again..

I tried on angularjs btw, but either $scope.moudule() or module() work for me.

Upvotes: 0

Views: 249

Answers (2)

cvsguimaraes
cvsguimaraes

Reputation: 13240

Self-execute the result of the attribution, you're storing the result of the self execution (in this case is nothing).

Try this:

(module = function () {
    console.log('hello world');
})();

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388406

since the function is not returning anything module is undefined, that is why you are getting the error.

The solution is to create a function then executed it

function module () {
    console.log('hello world');
};
module()

now module refers to a function which can be invoked later

Upvotes: 0

Related Questions