There are two js files file1.js and file2.js.Connected in the same sequence.

File1.js

'use strict'; function func1(){ //some code } function func2(param1){ func1(); //some code } function func3(param1){ //some code } 

File2.js

 'use strict'; $(function () { $('.btn').click(function (e) { var t=$(e.target); func2(t); }); $('.btn2').click(function (e) { func3(e); }); } 

I get the following errors:

//file2.js

func2 is not defined.

func3 is not defined.

//file1.js

func3 is defined but never used.

Where is my mistake?

  • Do you have a wrapper (IIFE) for the func2 and func3 functions func2 func3 ? Also make sure that the File1.js file is successfully downloaded. - Stepan Kasyanenko
  • Why this construct $(function () {} ? - Sergey Gornostaev
  • That's just the point, do not wrap. Try writing window.func2 in the console after the page loads. If it returns undefined , then either File1.js does not load, or you have syntax errors, or there is a wrapper in File1.js over func2 . - Stepan Kasyanenko
  • It `s Magic! Try creating an example that reproduces the problem. For example, on jsfiddle.net . - Stepan Kasyanenko

1 answer 1

At the beginning of File1.js, declare functions globally.

 window.func2 = func2; window.func3 = func3; //Ваш код