我在我的javascript文件上运行了ESLint工具。我已经解决了大部分问题,但我无法修复一条评论

时间:2011-01-24 12:56:59

标签: javascript jslint

我在我的java脚本文件上运行了JSLint工具。我已经解决了大部分问题,但我无法修复一条评论。

隐含的全球:

  • 文档 1,4,7,10,31,34,38,58,61,64,67,74,103,106,109,432,441,450,
  • 确认364,
  • hideErrorMessageFields 403,
  • spanBusinessDivisionValidate 409,
  • spanBusinessGroupValidate 418,
  • validatePeoplePicker 425,434,452,
  • spanPeopleWorkingOSSValidate 427,
  • spanRequiredGMAliasValidate 436,

如何修复此评论?

3 个答案:

答案 0 :(得分:3)

您将documentconfirm等称为全局命名空间中存在的内容。

JSLint并不神奇地知道他们在那里。因此,您可以使用global选项通过添加

将它们声明为已知的全局变量

/*global document confirm etc*/

到文件的顶部。这需要是一个注释,并且计算一个空间分隔的名称,变量,函数等列表,您知道它们是全局的。然后JSLint会停止警告你它不知道它们。

答案 1 :(得分:0)

将“假设浏览器”标志添加到JS文件的顶部:

/*jslint browser:true*/

来自JSLint

  

可以为其预定义一些全局变量   您。选择“假定浏览器”   (浏览器)选项预定义   标准的全局属性   由Web浏览器提供,例如   document和addEventListener。它有   与此评论相同的效果:

     

/ * global addEventListener:false,   blur:false,clearInterval:false,   clearTimeout:false,close:false,   closed:false,defaultStatus:false,   document:false,event:false,focus:   false,frames:false,   getComputedStyle:false,history:   false,Image:false,length:false,   location:false,moveBy:false,   moveTo:false,name:false,navigator:   false,onblur:true,onerror:true,   onfocus:true,onload:true,onresize:   true,onunload:true,open:false,   opener:false,选项:false,parent:   false,print:false,resizeBy:false,   resizeTo:false,screen:false,   scroll:false,scrollBy:false,   scrollTo:false,setInterval:false,   setTimeout:false,status:false,top:   false,XMLHttpRequest:false * /

答案 2 :(得分:0)

  JSLint documentation says: 

Undefined Variables and Functions

JavaScript's biggest problem is its dependence on global variables, particularly implied global variables. If a variable is not explicitly declared (usually with the var statement), then JavaScript assumes that the variable was global. This can mask misspelled names and other problems.

JSLint expects that all variables and functions are declared before they are used or invoked. This allows it to detect implied global variables. It is also good practice because it makes programs easier to read.

Care for that error. Nearly every coding convention wants you not to use implied globals. 

Variables can be declared using the var keyword