jslint Import global variables from another file

时间秒杀一切 提交于 2019-12-01 06:45:24

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.

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