2

I'm currently working on a project with Kendo UI, jQuery and Symfony 1.4 and I have some global variables.

<script type="text/javascript">
  var addURL = '<?php echo url_for('dashboard/addEmployee') ?>';
  var deleteURL = '<?php echo url_for('dashboard/deleteEmployee') ?>';
  var editURL = '<?php echo url_for('dashboard/editEmplouee') ?>';
  var viewURL = '<?php echo url_for('dashboard/viewEmployees') ?>';
      ... some other URLs follow here.
</script>

And I would use those variables as follows:

$.ajax({
  url: addURL,
  .. other options here
});

Let's say for example that I have many global variables maybe a hundred, do I need to worry about "destroying" those variables? Since I read somewhere that javascript is garbage collected. Will those variable be collected by the GC when I change the page?

And lastly about with kendo UI, I'm using the Kendo UI widget Window to act as a dialog in my project, how would I handle the dialog after it is closed? Because I'm reusing the same dialog all around from information messages to error messages. Is it lost/destroyed (out of scope) if the click handler of an element is over?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Durty
  • 457
  • 1
  • 7
  • 14
  • Checkout this page it should help: http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection – Chedy2149 Dec 24 '12 at 09:18
  • One notice: if you want to keep so many global variables, you definitely do better if you use it through global object like `var myGlobals = { addUrl: 'something1', viewUrl: 'something2' };` – Jan.J Dec 24 '12 at 09:20
  • Thank you Chedy2149 and yabol, I was just worried about some memory leaks. – Durty Dec 24 '12 at 09:31

1 Answers1

2

"... do I need to worry about 'destroying' those variables?"

I don't know whether you need to worry or not but global variables defined with var name = expression cannot be deleted either manually with delete, or by Garbage Collection (GC).

If you want to define your strings in such a way that the can be deleted individually, then define them as properties of an object. If you want to know more about the use of javascript's delete then read this.

If you want to ensure that your strings become available to GC, then define them inside a function, and also ensure that the function (more accurately any "execution context" generated by execution of the function) is not part of one or more "closure" scope chains. By doing so, the function (and GC) will have to more work to do, so there's a tradeoff between memory and CPU load.

"Will those variable be collected by the GC when I change the page?"

Like the DOM, javascript's entire window object (the global namespace and everything in it) is destroyed on page change/reload. I'm not sure it's correct to say that GC does the destruction but it's academic - it's either GC or something very much like it.

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • Oh dear, I'm sorry to be the bearer of bad news. But rewriting code every is very normal so you are now officially a Real Programmer! :-) – Beetroot-Beetroot Dec 24 '12 at 17:35