Ryan
Ryan

Reputation: 10049

javascript: Global var leaking

Whenever I submit a plugin to Firefox I get an email telling me some of my variables are leaking into the global scope... once they tell me I fix the issue. But till then is there any way (program?) to check if the variables are leaking into the global scope?

Thanks!

Upvotes: 3

Views: 1940

Answers (5)

user278064
user278064

Reputation: 10170

Declare all your variables at the top of each function.

The worst of all of JavaScript's bad features is its dependence on global variables. A global variable is a variable that is visible in every scope. Global variables can be a convenience in very small programs, but they quickly become unwieldy as programs get larger. Because a global variable can be changed by any part of the program at any time, they can significantly complicate the behavior of the program. Use of global variables degrades the reliability of the programs that use them. [...] In most languages, it is generally best to declare variables at the site of first use. That turns out to be a bad practice in JavaScript because it does not have block scope. It is better to declare all variables at the top of each function.

"JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc., 978-0-596-51774-8."

Upvotes: 1

mjc1283
mjc1283

Reputation: 213

The ECMAScript standard 5th edition introduces an optional "strict mode", which forces you to use a cleaner subset of JS. In strict mode, if you try to assign to a variable that hasn't been declared with the var keyword, a ReferenceError will be thrown.

You can enable strict mode inside a function, or in the entire file, by including this as the first statement:

"use strict";

I'd recommend strict mode for your problem. It's already implemented in recent versions of major browsers. See here for an overview:

https://developer.mozilla.org/en/JavaScript/Strict_mode

Upvotes: 4

Mike Samuel
Mike Samuel

Reputation: 120516

The lint tools others have recommended are an excellent idea, and will point you at the problem, but if you have unittests, you can incorporate tests for leaked globals into them.

Just run this before you load your source code,

var globalsBeforeLoad = {};
(function () {
  for (var k in this) { globalsBeforeLoad[k] = true; }
})()

and then run this after you load your code

(function () {
  var leaked = [];
  for (var k in this) {
    if (!Object.hasOwnProperty.call(globalsBeforeLoad, k)) {
      leaked.push(k);
    }
  }
  if (leaked.length) { alert("You leaked " + leaked.join(", ")); }
})()

Upvotes: 3

Mrchief
Mrchief

Reputation: 76218

Any variable declaration inside a function that doesn't start with var will leak into global scope. You can check for those.

Upvotes: 2

Sean Vieira
Sean Vieira

Reputation: 159905

Both JSLint and JSHint will give you warnings when you miss a var. Both are available on Github (JSLint, JSHint) and both can be run from the command line using Node.js, Rhino or any other non-browser environment of your choice. They can even be integrated into your text editor / IDE of choice and run with a keystroke.

Upvotes: 4

Related Questions