...

View Full Version : Global var optimalisation



Jerome
01-10-2004, 02:54 PM
Hi,

What is the better of these 2:

1. Three global vars, like var1=Hello var2=Java_script var3=coders

2. A string in one global var, like: var1=Hello#Java_script#coders

Thanks for Your time,
Jerome

Dennis
01-10-2004, 05:16 PM
it dipendes on what you will do with that values...

Garadon
01-10-2004, 05:21 PM
if the value of the variables need to be accesed seperately I would go with 3 vars, since u then save from having to make string operations.

liorean
01-10-2004, 05:32 PM
How about:
var
aVars = ['Hello','JavaScript','Coders'];or:
var
oVars = {
first: 'Hello',
second: 'JavaScript',
third: 'Coders'
};

Jerome
01-10-2004, 07:52 PM
Thanks for Your effort!

I was also thinking about the global namespace necessary for each possibility, rather then the possible construction.

Jerome

liorean
01-10-2004, 08:14 PM
The rule is simple: Only put things in the global namespace if you need them to be present from the time the page is parsed to the time the page is unloaded. That way you aren't polluting the global namespace. My way, you store each of them separate but still use only one global namespace slot, unlike the three separate vars. And, they are separate properties, instead of written as part of one string as in your example.

Jerome
01-10-2004, 08:50 PM
Thanks Liorean!

That's what I want to know

Jerome



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum