Function, variable et al names

A

Andrew Poulos

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.

I have a friend who starts the name of every function with "f" eg.
fMyFunction, and every global variable with "g" eg. gMyVariable. I'm not
sure if this is the "smartest" way to do it.

Should I make a separate index with each name listed with a description
of it's purpose, usage etc?


Andrew Poulos
 
E

Evertjan.

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top