Click here to Skip to main content
16,020,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have 3 JS files in my project.
i wanted to create a global observable variable where i can access and update it from any of my existing JS files.

Please help me with a way to do this.
Posted

1 solution

First of all, from the standpoint of JavaScript code, there are no "different files". You include more then one file in HTML, and they are processed in the order they are included, as if it was one single file. So, the question about "different files" simply makes no sense. If your object is global, it will be global object in all files, the same one.

At the same time, using global objects is a pretty bad idea. You should better think at how to make more objects as local as possible, and how to pass them in local contexts, which can be done using function argument, including "this".

—SA
 
Share this answer
 
v3
Comments
Kornfeld Eliyahu Peter 22-Apr-15 2:58am    
The second paragraph itself worth a 5! (the first too, but I can give only once)...
Sergey Alexandrovich Kryukov 22-Apr-15 9:03am    
Thank you, Peter.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900