0

When I want to import a 'class' in node.js I do this at the beginning of the file:

var MyClass = require('./MyClass.js');

what if I do this:

MyClass = require('./MyClass.js');

without var? Is there a downside by not using var and placing this variable in global context?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Dave
  • 51
  • 2
  • 9
  • 5
    Put `'use strict';` at the top of your file, run your code and sit back and relax. – Rob W Jan 08 '14 at 14:40
  • Maybe I was not clear enough and the question is bit confusing. I know the difference by using and not using var in javascript. The questions targets the impact on node.js. For example what if I start the App multiple times, is the global scope shared between those apps? – Dave Jan 08 '14 at 14:57
  • Thanks Mahan and Jason, if I could I would mark both explanations as an accepted answer. – Dave Jan 08 '14 at 15:20

3 Answers3

3
  • if you declare a variable in a global scope, putting var or not has no difference
  • if you declare a variable with a var in a function then you create a local variable inside that function
  • if you create a variable without a var inside a function, it will look up the scope chain until it finds the variable or hits the global scope

Is there a downside by not using var and placing this variable in global context?

the biggest downside is you will clutter the global namespace that makes the code hard to maintain. there are a lot of things you must consider on declaring global variables especially in javascript you can have a further reading on this article http://c2.com/cgi/wiki?GlobalVariablesAreBad

another thing is you can stil refer to javascript resources when your concern is about node.js because node.js's primary language is javascript

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • That I understand. I'm trying to use closurecompiler for typechecking. There is a rule, saying, that variables always begins with lowercase. Since im importing 'Classes' (at the top of the file) I want to begin the variables with a uppercase letter. So by not using var works, but I would like to know the ramifications on node.js, if for example I start the node app multiple times, is the global context then shared between those apps? – Dave Jan 08 '14 at 15:07
  • what do you mean start node app multiple times? run a nodejs up in the console mulitple times? it will run on different threads and will not share anything – Netorica Jan 08 '14 at 15:09
  • Yeah, I meant starting multiple instances of an app. Thanks for your answer :) – Dave Jan 08 '14 at 15:16
  • Given your reasoning for wanting to use global scope, I would shed a tear, abandon the intellectual purity of capitalizing your variable the same as your import, and move on with life with proper locally scoped variables. Or use some other method for typechecking that has rules compatible with your project. Edit - And I would shed figurative tears all day long over that, intellectual purity leads to great code in my experience, but you have to be willing to give it up when it makes sense. – Jason Jan 08 '14 at 15:32
  • Well, I don't want to use global scope, I wasn't shure if node is doing anything different than plain js with global scope. I will stay with locally scoped variables and try some different workaraounds for my problem. Code is working fine, it's just the type checking that is missing. It is a nice to-have and would be great if it works. – Dave Jan 08 '14 at 16:26
2

To answer your question "is there a downside?", yes, there are downsides and there are upsides to using global scope... despite the ubiquitous warnings about global scope, you can use it effectively under some circumstances in some languages in some contexts if you plan accordingly, have enough information about your application and the various components that make it up, etc etc etc...

In my experience, node doesn't provide any compelling reasons to use global scope, so you're opening yourself up to the dangers without experiencing any of the potential benefits. You have to be mindful about passing your variables around, but that's the "node way", it's designed specifically to work that way and javascript in particular is very good about allowing you to do that in ridiculously powerful ways.

The short answer is that, if this is a module for inclusion in other projects, then you're courting disaster by introducing your variable into the global scope of projects where you couldn't hope to understand how that could effect things. If this is a standalone application, so long as it remains trivial and will not grow and evolve over time, you're probably OK using the global scope as a shortcut. The more your application grows and changes, the more likely you are to run into problems by not properly limiting scope.

Jason
  • 13,606
  • 2
  • 29
  • 40
0

Putting var in front of the variable definition defiles the variables in the local scope of the function you are in.

With no var the variable automatically goes into the global scope.

For more help see here.

Community
  • 1
  • 1
qwertynl
  • 3,912
  • 1
  • 21
  • 43
  • 1
    OP already knows that. Please check the question again. – thefourtheye Jan 08 '14 at 14:42
  • 1
    From the OP: "Is there a downside by not using var and placing this variable in global context?" I think the OP does know that. The question seems to be more about the downside of the global scope, although it's not phrased particularly artfully. – Scott Sauyet Jan 08 '14 at 14:53