Call functions from function inside an object (object literal)

holyredbeard picture holyredbeard · Nov 21, 2011 · Viewed 67k times · Source

I'm learning to use object literals in JS, and I'm trying to get a function inside an object to run by calling it through another function in the same object. Why isn't the function "run" running when calling it from the function "init"?

var runApp = {

    init: function(){   
         this.run()
    },

    run: function() { 
             alert("It's running!");
    }
};

Answer

Matt Ball picture Matt Ball · Nov 21, 2011

That code is only a declaration. You need to actually call the function:

runApp.init();

Demo: http://jsfiddle.net/mattball/s6MJ5/