Mad_Griffith Posted December 9, 2016 Share Posted December 9, 2016 (edited) Hi, I am trying to have FILE2.JS override FILE1.JS, but can't find the right syntax to do this (please also note I should first try and modify FILE2.JS only): FILE1.JS $(document).ready(function () { var weare = [ "Young and free" ]; }); FILE2.JS (function (jQuery) { var weare = [ "inspired by music" ]; })(jQuery) Edited December 9, 2016 by Mad_Griffith Link to comment Share on other sites More sharing options...
justsomeguy Posted December 10, 2016 Share Posted December 10, 2016 One variable cannot override the other, they exist in two separate and unrelated scopes. Just because they have the same name doesn't mean anything, you do not have access to the local variables inside the function from anywhere except inside the function. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted December 10, 2016 Author Share Posted December 10, 2016 How can I change the second bit to make the second variable override the previous one? Link to comment Share on other sites More sharing options...
Ingolme Posted December 10, 2016 Share Posted December 10, 2016 You cannot without also modifying the first code. The variables are declared in different scopes. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted December 10, 2016 Author Share Posted December 10, 2016 Ok. How can this be done by editing FILES1 too? Link to comment Share on other sites More sharing options...
Ingolme Posted December 10, 2016 Share Posted December 10, 2016 By making the variable global. To make a variable global, just declare it outside of any function and don't use the var keyword inside the functions. var weare; $(document).ready(function () { weare = [ "Young and free" ]; }); (function (jQuery) { weare = [ "inspired by music" ]; })(jQuery) Link to comment Share on other sites More sharing options...
Mad_Griffith Posted December 10, 2016 Author Share Posted December 10, 2016 Thank you! Great, I thought that wouldn't work with jquery document.ready(). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now