Call functions from function inside an object (object literal)

杀马特。学长 韩版系。学妹 提交于 2019-11-28 18:12:53

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

runApp.init();

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

There is nothing magical about the init property of an object, which you happen to have assigned a function to. So if you don't call it, then it won't run. No functions are ever executed for you when constructing an object literal like this.

As such, your code becomes this:

var runApp = {
    init: function(){   
         this.run()
    },
    run: function() { 
         alert("It's running!");
    }
};

// Now we call init
runApp.init();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!