Why can external JavaScript files access other external JavaScript files functions and how to stop it? -
i no means guru javascript, apologies if questions sounds "beginner". why if have 3 javascript files in project, of 3 javascript files can call of other functions in other javascript files?
bigger question how can stopped? example if, of these 3 hypothetical javascript files, named managerjs.js, teamajs.js , teambjs.js, don't want teamajs able access teambjs want them both able access managerjs.
all scripts on page evaluated in global scope. there single shared global scope per page.
you can prevent access not defining stuff in global scope. can use iife create private scope each script:
// myscript.js (function() { // declared here *local* function var localvalue = 42; // expose values explicitly creating global variables // (but there other, more elaborate ways) window.globalvalue = 21; }());
only expose values want other code / scripts access. there various ways that, 1 of them revealing module pattern.
Comments
Post a Comment