I have three files:
- my_function.js
- scripts_1.js
- scripts_2.js
my_function.js
function myFunction() {
console.log('my function');
}
How can I use this function in both of my files scripts_1.js and scripts_2.js? I use these two in users.js as:
require('scripts_1');
require('scripts_2');
and in admins.js I use only one:
require('scripts_2');
Later in my webpack.mix.js I compile the users and admin files to two separate minified js files.
If I just use the require('my_function') in my scripts_1 and scripts_2 files this doesn't work (probably the myFunction() is not in the same scope).
However I can make this work if I attach my function to the window variable so that the my_function looks like this now:
window.myFunction = function() { ... }
At this point I don't even have to require it in any of the scripts_1 and scripts_2 files (but I do in my users.js and admin.js files) but this feels wrong somehow.
tldr;
I'd like to know how to define a helper js function that I can reuse in multiple other js files by using Laravel MIX.