Andrew Poulos wrote on 29 dec 2004 in comp.lang.javascript:
> If I'm writing a sizeable chunk of javascript (1000s of lines) and it
> includes lots of custom functions, global variables, prototypes etc is
> there an agreed, customary, conventional, or best practise way to name
> them? What I'm worried about is inadvertently having a naming clash
> where, say, a function and a global variable end up being named the same.
Write modular code that has its own defined local variables, and keep the
global variables to a minimum.
Enter the modules [=functions] with all variables as parameters and exit
the code with all parameters in the return value. The latter is not that
easy if there are more than one return values, but could be done with an
array.
That way the modules can even be reentrant.
The hopefully very few global variables could be given special names,
but that is not necessary,
as there is no strict objection to duplicate variable names:
<script type='text/javascript'>
var v = 7; //global
function f(){
var v = 8; //local
alert('local v = ' + v)
alert('global v = ' + window['v'])
}
f()
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
|