What is best practice to get a variable outside of its anonymous function without polluting the global namespace?
What is best practice to get a variable outside of its anonymous function without
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A number of possibilities:
Which makes the most sense depends upon how much data you need to share, how widely it needs to be shared, whether the sharing is both ways, etc…
The typical design pattern for exposing global data with the minimum impact on polluting the global namespace is to do something like this:
Then, anywhere in your app, you can call
JF.getMyData();or accessJF.myPublicData.The idea here is that all public methods (or even data objects) can be hung off the JF object so there’s only one new item in the global space. Everything else is inside that one object.