0





72
1

I have two .js files. On the first .js file i declare and execute a function like this:

(function($){
  function something(){
    //code here
  }

  $(document).ready(function(){
    something();
  });
})(jQuery);

Also, I want to execute the something() function in the second .js. My code is the following:

(function($){
  $(document).ready(function(){
    //more code
    something();
    //more code
  });
})(jQuery);

However, when the program is executed, I get the following error: Uncaught ReferenceError: something is not defined(…)

Possible Solution: I solved the problem declaring the function as:

(function($){
  this.something = function(){
    //code here
  }

})(jQuery);

Is this 100% right?

Question author Themis93 | Source

0


1

It's normal, you declared your function something in an IIFE, so you can't using it outside your block. Try to declare your function outside (function($) ...)() block.

Answer author Soywod

Ask about this question here!