I'd love to put the variable declarations in a function so they don't take a whole bunch of space, but is something similar to that even possible?
If you mean:
function one() {
var a, b, c, d, e, f;
// ...
}
function two() {
var a, b, c, d, e, f;
// ...
}
(E.g., each function has its own copy of the variables.)
...and you want
// Doesn't work
function declare() {
var a, b, c, d, e, f;
}
function one() {
declare();
// ...use a, b, etc. here
}
function two() {
declare();
// ...use a, b, etc. here
}
No, you cannot do that.
You might consider encapsulating the variables in an object, if the same set of variables is used in multiple places. In fact, the whole thing sounds like it might be refactored as an object.
But without too much refactoring:
function declare() {
return {
a: /*...*/,
b: /*...*/,
c: /*...*/,
d: /*...*/,
e: /*...*/,
f: /*...*/,
};
}
function one() {
var o = declare();
// ...use o.a, o.b, etc.
}
function two() {
var o = declare();
// ...use o.a, o.b, etc.
}
But again, that's the minimal-impact solution. It's like that if you look at your overall code with the idea that you could organize things a bit into objects, you'd find a less artificial way than the above.