jslint从另一个文件导入全局变量 [英] jslint Import global variables from another file

查看:67
本文介绍了jslint从另一个文件导入全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用jslint分析文件时,是否可以使用在另一个js文件中声明的全局变量.

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... */ 

是否有一种方法可以导入其他脚本文件,例如在Re-sharper中?

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

/// <reference path="my.js" /> 

推荐答案

实际上,JSLint可能在为您建议有用的代码体系结构改进.为什么不将所有这些全局变量放在同一个命名空间中?

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

而不是...

var Global1 = "spam",
    Global2 = 2;

...使用...

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

...在每个[other]文件上和获利.如果以后再向MyStuff添加更多项目,则已经涵盖了.而且,如果您需要在页面上的MyStuff中添加一些东西来将其视为全局元素,那也很简单... MyStuff.NewField = "new";

... 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";

您有很多东西从一个文件移到另一个文件,这已经表明它们是每个文件都需要了解的功能单元(或几个功能单元). JSLint建议您对它们进行分组.

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.

这篇关于jslint从另一个文件导入全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆