In JavaScript, since global variables are available to functions, is there ever any reason to pass them to functions through arguments?
April 7, 2012 10:06 AM Subscribe
In JavaScript, since global variables are available to functions, is there ever any reason to pass them to functions through arguments?
Best answer: Makes it much easier to see at a glance what the function modifies.
When you are hunting down a bug late at night you will be glad of it!
posted by emilyw at 10:11 AM on April 7, 2012
When you are hunting down a bug late at night you will be glad of it!
posted by emilyw at 10:11 AM on April 7, 2012
Best answer: Just because a language gives you features that allow you to write bad code, it doesn't mean you're required to use them.
posted by Obscure Reference at 10:13 AM on April 7, 2012 [8 favorites]
posted by Obscure Reference at 10:13 AM on April 7, 2012 [8 favorites]
Best answer: Testing. You should be (able to be) writing code suites that exercise your functions and make sure they do what you think they're doing. The only way to write the fifty ways your code can break and try them all is to make the function take parameters that you construct before you call it, and test the result once it returns.
posted by cmiller at 10:50 AM on April 7, 2012
posted by cmiller at 10:50 AM on April 7, 2012
Best answer: Douglas Crockford on JavaScript:
All variables should be declared before used. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may become implied globals. Implied global variables should never be used.And in Global Domination, again by Crockford:
...
JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages. Define all variables at the top of the function.
Use of global variables should be minimized. Implied global variables should never be used.
The global object is the global namespace that holds the top level functions and global variables. Variables which are not explicitly defined are implied global variables, and their names are also kept in the global object. This was a convenience for the little scripts that Navigator 2 was expected to support. In the decade since NS2, those little scripts have grown into Ajax applications, and the true nature of the global object is revealed:posted by artlung at 10:53 AM on April 7, 2012 [4 favorites]
Global variables are evil.
Global variables are a source of unreliability and insecurity. Fortunately, JavaScript includes tools for allowing us to drastically minimize our use of globals, which makes our programs more robust. This becomes increasingly important as our programs get bigger, and as we mix in and mash up program components from multiple authors. Reducing our dependency on globals increases the likelihood that collisions are avoided and that the program components work harmoniously.
An objective measure of the quality of a JavaScript program is How many global variables and global functions does it have? A large number is bad because the chance of bad interactions with other programs goes up. Ideally, an application, library, component, or widget defines only a single global variable. That variable should be an object which contains and is the root namespace for all of your stuff.
Best answer: Using functions also encourages code organization. One of the main questions you should be asking yourself when you design software is “How much code do I have to read to understand what this does?” When you find a bug in the global variable, you might have to search your entire codebase and read everything that could have modified it.
posted by yaymukund at 12:43 PM on April 7, 2012 [1 favorite]
posted by yaymukund at 12:43 PM on April 7, 2012 [1 favorite]
Best answer: Lots! Here's a concrete example: Suppose you have a loop that used the variable i, that is global. And then you call another function inside the loop which also uses your variable i. Then that's going to interfere with the value of i, and break everything in mysterious and irritating to debug ways.
Generally speaking, using global variables makes your code harder to understand and debug in exactly the way. If you really need globals for some reason, it's a good idea to put them all inside a big global variable like MYAPPLICATION so that it's obvious that they're global when you're referring to them. (as artlung mentioned)
posted by oranger at 4:19 PM on April 7, 2012
Generally speaking, using global variables makes your code harder to understand and debug in exactly the way. If you really need globals for some reason, it's a good idea to put them all inside a big global variable like MYAPPLICATION so that it's obvious that they're global when you're referring to them. (as artlung mentioned)
posted by oranger at 4:19 PM on April 7, 2012
Best answer: Well, one specific case where you might pass a global variable to a function is where that function will return another function that captures the current value of that variable; here's a contrived example:
The returned functions are called "closures" and this is a very common JavaScript programming technique.
Note that global variables are indeed evil and this is not code I would ever really write ツ
posted by nicwolff at 4:25 PM on April 7, 2012
function makeHelloFn (name) {
return( function () { console.log( "Hello " + name + "!" ) } );
}
foo = "Nic";
helloNic = makeHelloFn(foo);
foo = "Judy";
helloJudy = makeHelloFn(foo);
helloNic(); // logs "Hello Nic!"
helloJudy(); // logs "Hello Judy!"
The returned functions are called "closures" and this is a very common JavaScript programming technique.
Note that global variables are indeed evil and this is not code I would ever really write ツ
posted by nicwolff at 4:25 PM on April 7, 2012
Just to give a specific example, although the $ variable is typically associated with jQuery it's also used by other libraries, so often you'll see the "jQuery" variable passed in and assigned to "$" for jQuery-specific functions,
(function($){posted by holloway at 6:19 PM on April 7, 2012
// stuff
}(jQuery))
Best answer: I interpreted this question differently than everyone else. I don't generally declare global variables myself (except in the case of things like (var mainController = new ApplicationController();), but that's not really what I thought the question was asking.
If I'm writing a function that needs to operate on a global object, like "window" or "document", I won't bother passing those to the function unless there's some sort of special case (like the function might need to operate on a document from a different frame or something).
I agree with everyone that global variables should be avoided, but since some of them are core to using JavaScript, using them as they are in the global namespace is normally fine.
posted by tylerkaraszewski at 8:24 PM on April 7, 2012
If I'm writing a function that needs to operate on a global object, like "window" or "document", I won't bother passing those to the function unless there's some sort of special case (like the function might need to operate on a document from a different frame or something).
I agree with everyone that global variables should be avoided, but since some of them are core to using JavaScript, using them as they are in the global namespace is normally fine.
posted by tylerkaraszewski at 8:24 PM on April 7, 2012
This thread is closed to new comments.
posted by iotic at 10:09 AM on April 7, 2012