coding style - JavaScript: Two separate scripts - share variables? - Stack Overflow
Variable title in your example is declared as a global variable, therefore it will be available to any and all scripts loaded into the same page. Whats more, if there is already a global variable named title
on the same page, its value will be overwritten when you assign it the value "Hello World!"
The usual practice to avoid this sort of problem is to declare exactly one global variable, then put all of your other variables inside it. For example:
var bobbyS_vars = { title: "Hello World!"; };
Assign that lone global variable a name that no one else is likely to choose, such as your name or employer's name or best-of-all, a domain name that belongs you or your employer.
Another, more common way to handle this problem is to take advantage of of the way that JavaScript handles variable scope within functions. For example, create an anonymous function, declare all of your code inside that function, then call the function at the end of the declaration by putting () at the end of the declaration. For example:
Read full article from coding style - JavaScript: Two separate scripts - share variables? - Stack Overflow
No comments:
Post a Comment