Domanda

Is there a way to use global variables declared in another js file when analyzing the file using jslint.

Currently I have to declare all my global variables in the header, however that's really slow and not practical.

/* global console, myglobalvar1, othervar... */ 

Is there a way to import the other script file like in Re-sharper?

/// <reference path="my.js" /> 
È stato utile?

Soluzione

JSLint is probably suggesting a useful code architecture improvement for you here, actually. Why not put all of those globals in the same namespace?

Rather than...

var Global1 = "spam",
    Global2 = 2;

... use...

var MyStuff = MyStuff || {};
MyStuff.Global1 = "spam";
MyStuff.Global2 = 2;

... or, more conventionally...

var MyStuff = {
    Global1: "spam",
    Global2: 2 
};

... and now you can simply include...

/*global MyStuff*/

... on every [other] file and profit. If you add more items to MyStuff later, you're already covered. And if you need to add something to MyStuff on the page that's treating it as a global, that's straightforward too... MyStuff.NewField = "new";

That you have lots of stuff moving from one file to another already suggests they're a functional unit (or several functional units) that each file needs to know about. JSLint is suggesting you group them as such.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top