简体   繁体   中英

call a function inside a function from another function

I'm using jquery and in my app.js file I have tow main functions:

  1. $(document).ready(function (){});
  2. $(window).on("load", function (){});

my code looks like this:

 $(function () { // code // code function myFunction(){ // code } }); $(window).on("load", function () { // I want to use myFunction() here but I cant because it's not accessible from here myFunction(); }); 

is there a way to do this?

window.lib = {
  onWindowLoad: function () {
    ...
  },

 onDocReady: function() {
   ...
  },

  evenmore: {
    nested: function(){
     ...
    }
  }
}

$(window).on("load", function () {
  lib.onWindowLoad();
});

There would be many other ways to do so.

In case, if you need to read something somewhere, that's here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM