0

Can this be done either via writing some code to intercept the global scope default declaration or via a setting within the javascript engine itself which each program or function has access to?

What I mean is if I could, at the top of the program (or function), tell the javascript engine to use local scope for undeclared variables instead of global scope... then, when the javascript engine compiles each function, it would declare them in local scope.

I understand if you want to declare a variable outside of the current scope, that should be done explicitly since no one knows where you want it declared, but I think defaulting to global scope leaves too much room for error and forces too much typing of var statements in each function, so I'd like to change the default in my programs if that's possible.

My primary purpose is to avoid having to type the list of local variables in a var statement at the top of every function, and to avoid the maintenance of keeping this list in sync with the local variables as I'm using them within a function. Not allowing a global scope default also reduces the risk of errors.

I would never want undeclared variables to end up in any higher scope than the scope in which they are first used. There is no value in having them default to global scope that I can see.

I know I can explicity declare variables in the current scope (or the global scope), but would like to avoid doing so and use a more useful default than the global scope default built into javascript.

I also know I can use "use strict" to force all variables to be declared explicitly. Not wanting to debate the right or wrong of it, but just asking if there is any way to change the default to local scope instead of global scope for a given function.

ciso
  • 2,606
  • 4
  • 28
  • 54
  • 1
    I am not aware of such an option. But you could `use strict` to limit the possibilities. – Sebas Jan 11 '15 at 23:10
  • 2
    what is the underlying issue that has you looking down this path? – charlietfl Jan 11 '15 at 23:11
  • 3
    Use [CoffeeScript](http://coffeescript.org), it solves this particular annoyance and makes Javascript syntax more palatable overall IMO. – deceze Jan 11 '15 at 23:12
  • 1
    Maybe I'm not just not understanding your question, but what does declaring undeclared variables mean? – wmock Jan 11 '15 at 23:18
  • You should use strict mode, it will throw on using undeclared variables (instead of creating global ones on assignment). – Bergi Jan 11 '15 at 23:27
  • 4
    downside is chasing scope bugs, doesn't make a lot of sense to me and seems like you are fighting the language instead of embracing it – charlietfl Jan 11 '15 at 23:32
  • 1
    There isn't , and as long as you declare variables you are in control of the scope. Closure is a good thing – charlietfl Jan 11 '15 at 23:38
  • 1
    *" Chasing scope bugs is also an issue with the default global declaration"* That's what 'use strict' solves. – Felix Kling Jan 12 '15 at 00:01
  • No, there is no way to make JavaScript behave this way. Declare all variables. – meager Jan 12 '15 at 00:01

1 Answers1

1

No, Javascript behaves the way it does, there's no switch to flip to turn its variable declaration rules on its head.

'use strict' is an option you can employ to help you catch problems more easily, but it doesn't change the rules.

What you want is a different language; for example CoffeeScript, a Javascript preprocessor which, among other things, happens to solve this variable declaration issue explicitly.

deceze
  • 471,072
  • 76
  • 664
  • 811
  • 1
    IMO CoffeeScript is to JavaScript as LESS/SASS is to CSS. Once you got used to it, it's unimaginable going to back to the bare metal way of writing things. It's so well integrated into my IDE that there isn't even any additional burden; I just write code in CoffeeScript, my IDE compiles it transparently, I use .js files as usual in my code. Chrome's debugger can even transparently map back to the original CoffeeScript code, it's marvellous. – deceze Jan 12 '15 at 01:32