jslint Import global variables from another file

岁酱吖の 提交于 2019-12-01 05:13:56

问题


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" /> 

回答1:


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.



来源:https://stackoverflow.com/questions/20220634/jslint-import-global-variables-from-another-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!