tawheed
tawheed

Reputation: 5821

Self invoking function in javascript does not seem to work

After reading about self invoking functions I decided to take it for a spin, wondering why this example code does not invoke after it has loaded.

var App = App || {};

(function() {
    'use strict';

    App.MainUtility = {
         sayHello: function() {
           alert('Hello from the main utility');
        }
    };
    return App.MainUtility;
})();

Is there a chance that I am not understanding something properly?

Upvotes: 1

Views: 56

Answers (1)

JaredPar
JaredPar

Reputation: 754525

In this case your self executing function has done 2 things

  1. Defined a property named MainUtility on App
  2. Returned the property

At no point was it invoked hence nothing is expected to be displayed. You can display it though by adding the following line in place of return

App.MainUtility.sayHello();

Upvotes: 2

Related Questions