How can change this Javascript to not use global variables?
-
I'm having some trouble working out how to control program flow and not use global variables with the Javascript in my web app. In this example, when get_notes() is called, the ids of the received notes are stored in the current_note_ids array. When add_to_discussion() is called, current_note_ids is sent as a parameter to the server request. How can I do this without having current_note_ids as a global variable? <script type="text/javascript"> var current_note_ids = []; function add_to_discussion(){ $.post('/add_to_discussion',{current_note_ids:current_note_ids}); } function get_notes(){ $.post('/get_note_combination',{}, function(data) { current_note_ids = []; // clear existing note details for (i in data.notes) { current_note_ids.push(data.notes[i].guid); } } } $(document).ready(function() { $('#add_to_discussion_button').click(function(){ add_to_discussion(); return false; }); $('#get_notes_link').click(function(){ get_notes(); return false; }); }); </script>
-
Answer:
This removes all that code from the global scope using a https://developer.mozilla.org/en/JavaScript/Guide/Closures (function () { var current_note_ids = []; function add_to_discussion(){ $.post('/add_to_discussion',{current_note_ids:current_note_ids}); } function get_notes(){ $.post('/get_note_combination',{}, function(data) { current_note_ids = []; // clear existing note details for (i in data.notes) { current_note_ids.push(data.notes[i].guid); } } } $(document).ready(function() { $('#add_to_discussion_button').click(function(){ add_to_discussion(); return false; }); $('#get_notes_link').click(function(){ get_notes(); return false; }); }); })();
ben at Stack Overflow Visit the source
Other answers
You can use anonymous function to make it OO-like. In this case, you can choose what to "expose". var notes = $(function() { var current_note_ids = []; function add_to_discussion() { $.post('/add_to_discussion', { current_note_ids: current_note_ids }); } function get_notes() { $.post('/get_note_combination', {}, function(data) { current_note_ids = []; // clear existing note details for (i in data.notes) { current_note_ids.push(data.notes[i].guid); } }) } return { add_to_discussion: add_to_discussion, get_notes: get_notes }; })(); $(document).ready(function() { $('#add_to_discussion_button').click(function() { notes.add_to_discussion(); return false; }); $('#get_notes_link').click(function() { notes.get_notes(); return false; }); });
William Niu
Related Q & A:
- how can change magento calendar?Best solution by Magento
- How can you map the differences between Javascript objects?Best solution by Code Review
- How to change javascript values to php?Best solution by stackoverflow.com
- How can I pass global variables into a function?Best solution by Stack Overflow
- How can it be more beneficial to use drugs then harmful?Best solution by Ask.Metafilter.Com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.