How to change variables elegantly?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.