JavaScript lib reference variables in core script?
We have an HTML page with some global JavaScript variables. We have some utility functions that manipulate these variables. Can you move these util functions to a separate开发者_StackOverflow中文版 JavaScript file and include the file using the <script>
tag? Wasn't sure if the functions in the external file could get/set the global variables that are defined in the main page?
Yes, you can do so by all means. All scripts referenced by the same HTML page have access to the same set of global variables and functions. Just keep in mind that the scripts are included in the order in which they appear.
Unfortunately yes. The whole problem with the global object is that it's "too global" exposing your site to many security risks.
A better idea would be to encapsulate data and methods into objects
.
精彩评论