Simplified Example:
// path/to/js/panel.js
$(function(){
var hidePanel = function(){
//hides the panel div
};
});
// path/to/js/another-script.js
$(function(){
hidePanel(); //out of scope MEGA-FAIL
});
As we speak, i have some functions/variables copy-pasted in 2 different files.
I was curious whether RequireJS would solve this problem…
Your function itself just needs to be declared after jQuery is loaded if it needs jQuery, but it need not be declared on
document.ready, just executed then. The simplest way to do what you’re after is:This just passes your function to
$(), which schedules it run ondocument.ready, or immediately if the document is already ready.