What nobody has really mentioned here is that "global" is a somewhat tricky
concept.  In fact, the DOM is in the global space, and therefore anything
attached to a DOM node is also in the global scope.

Someone else could, further down the line, write some code or use a not
brilliantly built jquery plugin that also does something like:

$('body').data("my_array", {.....});

totally wiping out your data.


If at all possible, follow Scott's advice and create your own scope, keeping
the data in there .... that's about as safe and "sandboxed" as things can
get with javascript.

You might find it's a reasonably easy conversion from this:

// main.js
MY_ARRAY = load_some_json();
.. further on ...
for ( var i in MY_ARRAY ) {}


to this:

//main.js
(function() {
  var MY_ARRAY = load_some_json(); // nb the "var" is really important here
  .. further on ...
   for ( var i in MY_ARRAY ) {}
})();


The key idea is that "MY_ARRAY" *only* exists inside that
"self-calling-anonymous-function".  It cannot be overwritten by anything
else calling itself "MY_ARRAY" somewhere outside of your code.

This kind of thing is usually pretty trivial if you start out working in
this fashion, and if you're finding it tricky because you want access to the
value from outside of your closure, and your code doesn't make that fairly
easy, you might want to think again about the architecture.  Two options
there would be a functional style (always passing the values you want your
functions to operate on) or an injection style (allowing the creation of
"instances" which can have values set on them).



On 29 September 2011 15:39, Sergio Ricardo <sergio.ricard...@gmail.com>wrote:

> In my opinion discard the data, saving the cost of a DOM access.
>
>
> 2011/9/29 Šime Vidas <sime.vi...@gmail.com>
>
>> > Sime Vidas, I see your point, but how is that better than the 2 options
>> i
>> > presented? Is it better? why use your option and not one of the 2 that i
>> > provided?
>>
>> Well, a global variable is out of the question, you want to avoid
>> global variables.
>>
>> As for using .data(), that depends on the "scope" of the array. Is the
>> information in the array relevant to the entire web-page, or just that
>> one element?
>>
>> --
>> To view archived discussions from the original JSMentors Mailman list:
>> http://www.mail-archive.com/jsmentors@jsmentors.com/
>>
>> To search via a non-Google archive, visit here:
>> http://www.mail-archive.com/jsmentors@googlegroups.com/
>>
>> To unsubscribe from this group, send email to
>> jsmentors+unsubscr...@googlegroups.com
>>
>
>  --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/jsmentors@jsmentors.com/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/jsmentors@googlegroups.com/
>
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com
>



-- 
Pete Otaqui
p...@otaqui.com
+44 7949 945542

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to