Is there is any way/tool to detect the duplicated variables/methods names in the project JavaScript files?
+2
A:
There is no such thing as duplicate names in Javascript. You will never get an error when re-declaring a name that already exists.
To avoid overwriting existing names in Javascript, good developers do at least one of these things:
1) Carefully keep their variables out of the global scope, usually adding all names needed for the app under just one or two globally-scoped objects.
// No
var foo = 1;
var bar = 2;
var bas = 3;
// Yes
var MyApp = {
foo:1,
bar:2,
bas:3
}
2) Check that a variable name does not yet exist before creating it.
// No
var MyObj = {};
// Yes
var MyObj = MyObj || {} // Use MyObj if it exists, else default to empty object.
Triptych
2009-06-10 15:16:48