Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


欢迎!

JSLint 以及其流行的另一分支, JSHint, 好像对于他们会「hurt your feelings」感到自豪。 好吧,这没有任何问题,但是如果了解一下为什么会这哥俩会「hurt feelings」,这应该是一个不错的主意。

JSLint Error Explanations 的目的是通过理解一些由JSLint和JSHint产生的「神秘」问题来帮助我们提高JavaScript水平,当然,还有如何预防此类错误。

这个站点仍然在不断更新中,因此并不是所有的JSLint或者JSHint的错误消息都在此列出。代码托管于GitHub,这里是英文原版的托管地址 如果你想要帮助我翻译、改善这个站点,可以发送邮件给我或者在GitHub上提出issues或者pull request。


JSLint & JSHint Error Messages

Search
  • JSLint JSHint '{a}' is a statement label

    JSLint and JSHint will throw the "'{a}' is a statement label" error when it encounters a reference that shares an identifier with a label defined in the same scope. In...

  • JSLint ADsafe ADsafe violation: '{a}'

    JSLint (prior to version 2013-03-18) will throw the "ADsafe violation: '{a}'" error in a number of situations, but only when the adsafe option is set to true (it's false by...

  • JSLint ADsafe ADsafe autocomplete violation

    JSLint (prior to version 2013-03-18) throws the "ADsafe autocomplete violation" error when the adsafe and fragment options are both set to true (they're false by default) and you pass it...

  • JSLint ADsafe ADSAFE violation: bad id

    JSLint (prior to version 2013-03-18) throws the "ADSAFE violation: bad id" error when the adsafe and fragment options are set to true (they're false by default) and you pass it...

  • JSLint ADsafe ADsafe violation: Wrap the widget in a div

    JSLint (prior to version 2013-03-18) throws the "ADsafe violation: Wrap the widget in a div" error when the adsafe and fragment options are set to true (they're false by default)...

  • JSLint ADsafe ADSAFE: Use the fragment option

    JSLint (prior to version 2013-03-18) throws the "ADSAFE: Use the fragment option" error when the adsafe option is set to true (it's false by default) and you pass it a...

  • JSLint ADsafe ADsafe violation: Misformed ADSAFE.go

    JSLint (prior to version 2013-03-18) throws the "ADsafe violation: Misformed ADSAFE.go" error when the adsafe and fragment options are set to true (they're false by default) and the call to...

  • JSLint ADsafe Currently, ADsafe does not operate on whole HTML documents. It operates on <div> fragments and .js files

    JSLint (prior to version 2013-03-18) throws the "Currently, ADsafe does not operate on whole HTML documents. It operates on <div> fragments and .js files" error when the adsafe and fragment...

  • JSLint Nested comment

    JSLint will throw the "Nested comment" error when it encounters the characters /* inside a multiline comment. Here's an example:/* This is a multiline comment./* It's valid JavaScript,/* but JSLint...

  • JSHint Attempting to override '{a}' which is a constant

    JSHint will throw the "Attempting to override '{a}' which is a constant" error when it encounters an assignment to a constant that is not preceded by the const keyword, when...

  • JSHint Option 'validthis' can't be used in a global scope

    JSHint will throw the "Option 'validthis' can't be used in a global scope" error when it encounters the validthis JSHint option in a global scope. Here's a silly example in...

  • JSLint JSHint Do not assign to the exception parameter

    JSLint (prior to July 2013) and JSHint will throw the "Do not assign to the exception parameter" error when they encounter an assignment inside a catch block to the identifer...

  • JSHint Bad escapement of EOL. Use option multistr if needed

    JSHint will throw the "Bad escapement of EOL. Use option multistr if needed" error when it encounters a multiline string. In the following example we attempt to assign a multiline...

  • JSLint JSHint Do not use 'new' for side effects

    JSLint and JSHint will throw the "Do not use 'new' for side effects" error when they encounter a function invocation preceded by the new operator when not part of an...

  • JSHint Wrapping non-IIFE function literals in parens is unnecessary

    JSHint (version 1.0.0 and above) will throw the "Wrapping non-IIFE function literals in parens is unnecessary" error when it encounters a function expression wrapped in parentheses with no following invoking...

  • JSLint JSHint Do not wrap function literals in parens unless they are to be immediately invoked

    JSLint will throw the "Do not wrap function literals in parens unless they are to be immediately invoked" error when it encounters a function expression wrapped in parentheses with no...

  • JSLint Combine this with the previous 'var' statement

    JSLint will throw the "Combine this with the previous 'var' statement" error when it encounters multiple variable statements within a function. Here's an example in which we attempt to declare...

  • JSLint JSHint Expected a conditional expression and instead saw an assignment.

    As of July 2013, JSLint has removed this warning and instead now issues the "Unexpected assignment expression" warning. More details may be found on the page for that message. JSLint...

  • JSHint const '{a}' has already been declared

    JSHint will throw the "const '{a}' has already been declared" error when it encounters a constant declaration with an identifier that has already been used in a previous constant declaration,...

  • JSHint A constructor name should start with an uppercase letter

    JSHint will throw the "A constructor name '{a}' should start with an uppercase letter" error when it encounters an identifier following the new operator, when the newcap option is set...

  • JSLint A constructor name '{a}' should start with an uppercase letter

    JSLint will throw the "A constructor name '{a}' should start with an uppercase letter" error when it encounters a function whose identifier begins with a lowercase letter and is later...

  • JSLint JSHint Unexpected dangling '_' in '{a}'

    JSLint will throw the "Unexpected dangling '_' in '{a}'" error when it encounters an identifier that begins or ends with the underscore character. JSHint will throw this error in the...

  • JSHint '{a}' is defined but never used

    JSHint will throw the "'{a}' is defined but never used" error when it encounters a variable or function declaration, or function arguments, with an identifier that is not referred to...

  • JSHint Forgotten 'debugger' statement?

    JSHint (version 1.0.0 and above) will throw the "Forgotten 'debugger' statement?" error when it encounters a debugger statement. In the following useless example we attempt to use the debugger statement:debugger;...

  • JSHint All 'debugger' statements should be removed

    JSHint (before version 1.0.0) will throw the "All 'debugger' statements should be removed" error when it encounters a debugger statement. In the following useless example we attempt to use the...

  • JSHint Variables should not be deleted

    JSHint will throw the "Variables should not be deleted" error when it encounters the delete operator when its operand does not appear to be an object property. In the following...

  • JSLint Only properties should be deleted

    JSLint will throw the "Only properties should be deleted" error when it encounters the delete operator when its operand does not appear to be an object property. In the following...

  • JSHint Duplicate key '{a}'

    JSHint (version 1.0.0 and above) will throw the "Duplicate key '{a}'" error when it encounters an object literal that contains more than one property with the same identifier. In the...

  • JSHint Duplicate member '{a}'

    JSHint (below version 1.0.0) will throw the "Duplicate member '{a}'" error when it encounters an object literal that contains more than one property with the same identifier. In the following...

  • JSLint JSHint Empty class

    JSLint and JSHint (prior to version 1.0.0) will throw the "Empty class" error when it encounters a regular expression literal containing an empty character class. Note that it will not...

  • JSHint ES5 option is now set per default

    JSHint (since version 2.0.0) will throw the "ES5 option is now set per default" error when it encounters the es5 JSHint option with a value of true. Here's an example...

  • JSLint This is an ES5 feature

    JSLint will throw the "This is an ES5 feature" error when it encounters a multiline string. In the following example we attempt to assign a multiline string to the variable...

  • JSHint eval can be harmful

    JSHint (version 1.0.0 and above) will throw the "eval can be harmful" error when it encounters an invocation of the eval function. Here's an example in which we use eval...

  • JSLint JSHint eval is evil

    JSLint and JSHint (prior to version 1.0.0) will throw the "eval is evil" error when they encounter an invocation of the eval function. Here's an example in which we use...

  • JSLint JSHint Expected an identifier and instead saw '{a}' (a reserved word)

    JSLint and JSHint will throw the "Expected an identifier and instead saw '{a}' (a reserved word)" error when they expect a reference to an identifier but finds a keyword that...

  • JSLint Expected exactly one space between '{a}' and '{b}'

    JSLint will throw the "Expected exactly one space between '{a}' and '{b}'" error when it encounters a number of spaces that is not equal to one in the following situations...

  • JSLint Expected a string and instead saw '{a}'

    JSLint (since late 2012) will throw the "Expected a string and instead saw '{a}'" error when it encounters a comparison operator in which one of the operands is a typeof...

  • JSHint Extra comma

    JSHint (version 2.0.0 and above) will throw the "Extra comma" error when it encounters a comma following the final element of an array or object literal, but only when the...

  • JSLint JSHint The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

    JSLint will throw the "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype" error when it encounters a for-in...

  • JSHint Function declarations should not be placed in blocks

    JSHint will throw the "Function declarations should not be placed in blocks" error when it encounters a function declaration inside a block statement. In the following example we attempt to...

  • JSLint Function statements should not be placed in blocks

    JSLint will throw the "Function statements should not be placed in blocks" error when it encounters a function declaration inside a block statement. In the following example we attempt to...

  • JSHint The Function constructor is a form of eval

    JSHint (version 1.0.0 and above) will throw the "The Function constructor is a form of eval" error when it encounters a call to the Function constructor function preceded by the...

  • JSLint JSHint The Function constructor is eval

    JSLint and JSHint (before version 1.0.0) will throw the "The Function constructor is eval" error when they encounter a call to the Function constructor function preceded by the new operator....

  • JSLint JSHint Don't make functions within a loop

    什么时候会产生这个错误? 如果在一个for, while或者do循环体中包含了函数体或者表达式,JSLint和JSHint会抛出"Don't make functions within a loop"的错误。在下面这个例子中,我们尝试着给所有同一个class名称的元素添加点击事件。事件处理函数的目的是要将被点击的元素的内容覆盖为循环迭代中的i的值:/*jslint browser: true, plusplus: true */var elems = document.getElementsByClassName("myClass"), i;for (i = 0; i 为什么会产生这个错误? 产生这个错误的原因是为了强调那些可能不会按照你预期执行的代码并且指出对这门语言如何运行的误解。如果不修改这个错误,你的代码也许可以正常运行没有任何错误,但是在某些情况下会表现的让人感到十分意外。 根本的问题在于JavaScript解释器会在每一次循环迭代的时候创建一个函数实例这样做的原因是JavaScript不知道函数对象是否会在别的地方被修改。由于函数在JavaScript里是标准对象,它们可以拥有像其他对象一样的、可以在循环过程中被改变的属性。因而在一个循环体的上下文环境中创建函数,就会让解释器产生很多函数实例,从而导致了怪异的行为以及问题。 在上面那个例子中,当你点击其中任意一个元素,随后触发了事件处理函数,元素中的内容会变成i的最终计算出来的值。如果在NodeList中有4个元素,那么每一个元素中的内容都会最终变成了"4"。 为了理解这个问题,我们需要捕获每一次循环中i的值,并确保这个值是当前循环时的点击事件产生的。 一种通常的做法就是将函数体包含在一个即时执行函数内(immediately invoked function, aka,...

  • JSHint Function declarations are not invocable. Wrap the whole function invocation in parens

    JSHint will throw the "Function declarations are not invocable. Wrap the whole function invocation in parens" error when it encounters a function declaration followed by a pair of invoking parentheses....

  • JSLint Function statements are not invocable. Wrap the whole function invocation in parens

    JSLint will throw the "Function statements are not invocable. Wrap the whole function invocation in parens" error when it encounters a function declaration followed by a pair of invoking parentheses....

  • JSHint Implied eval. Consider passing a function instead of a string

    JSHint (version 1.0.0 and above) will throw the "Implied eval. Consider passing a function instead of a string" error when it encounters a call to the setTimeout or setInterval functions...

  • JSLint JSHint Implied eval is evil. Pass a function instead of a string

    JSLint and JSHint (before version 1.0.0) will throw the "Implied eval is evil. Pass a function instead of a string" error when they encounter a call to the setTimeout or...

  • JSLint JSHint Use the isNaN function to compare with NaN

    JSLint and JSHint will throw the "Use the isNaN function to compare with NaN" error when they encounter a comparison in which one side is NaN. In the following example...

  • JSLint JSHint A leading decimal point can be confused with a dot: '{a}'

    JSLint (prior to July 2013) and JSHint will throw the "A leading decimal point can be confused with a dot: '{a}'" error when they encounter a numeric literal preceded by...

  • JSHint Missing '()' invoking a constructor

    什么时候会产生这个错误? JSHint在遇到一个new表达式后没有紧随一对括号时,会抛出"Missing '()' invoking a constructor"的错误。以下例子中,我们创建了一个内置的Date构造函数:var d = new Date; 为什么会产生这个错误? 这个错误的提出是为了强调一种约定的缺失。如果不解决这个错误你的代码可以正常运行,但是你却违背了代码规范和最佳实践。在ECMAScript5的规范告诉我们new表达式并不需要紧随一对括号。唯一必须要括号的情况是,构造函数中传递了参数 (ES5 §11.2):MemberExpression :    ...    new MemberExpression ArgumentsNewExpression :    MemberExpression    new NewExpressionArguments :    ( )    ( ArgumentList ) 语法可能会有一点让人感到疑惑,但是本质上而言根据上面提到的内容,无结尾的Arguments是可选的。如果你并不需要向构造函数传递任何参数,你可以不添加括号。但是很多代码规范会为了保持代码的统一性而推荐使用括号,而且这样可以更清楚的看到调用了构造函数。 考虑到普通函数(非构造)缺少括号会使得表达式会直接计算该函数的引用,而不是其返回值。而在构造函数处缺少了括号你的代码就会变得缺少自我理解。解决这个问题你可以简单的添加缺少的括号:var d = new Date(); There...

  • JSLint JSHint Missing 'use strict' statement

    什么时候会产生这个错误? JSLint在遇到一个函数没有包含strict模式的声明,以及其祖先作用域中没有包含该声明时,会抛出"Missing 'use strict' statement"的错误。JSHint也会在相同情况下抛出错误,但是仅在strict选项设置为true的时候。以下是一个没有运行在strict模式下的函数:/*jshint strict: true */function example() { var x = 10;} 为什么会产生这个错误? 这个错误抛出的主要原因是为了强调约定的缺失。但是随着JavaScript引擎的发展,这个错误会变得十分有用 -- 因为它会强调那些可能不会按照你所期望运行的代码区域,也许会产生一些致命的JavaScript错误。 "use strict"语句是一种可行的命令,它可以出现在一个程序或者一个函数的第一个语句 (ES5 §14.1):A Directive Prologue is the longest sequence of ExpressionStatement...

  • JSHint Mixed double and single quotes

    什么时候会产生这个错误? 在一个字符串字面量已经被单引号或者双引号包裹的前提下,JSHint(自版本r09)遇到一个字符串两边是双引号或者单引号(与之前解析到的字符串相反)时,会抛出"Mixed double and single quotes"的错误。当然,这只发生在quotmark选项被设置为true的情况下。在下面的例子中,我们尝试着将两个字符串字面量赋给变量x和y:/*jshint quotmark:true */var x = "My String", y = 'Another string'; 为什么会产生这个错误? 这个错误是为了强调规范的缺失。在JavaScript中,单引号与双引号是没有区别的。这一点在字符串字面量的语法中就已经解释的很清楚了(ES5 §7.8.4):StringLiteral ::    " DoubleStringCharactersopt "    ' SingleStringCharactersopt ' 唯一的区别就是DoubleStringCharacters无法包含另一个双引号,同理SingleStringCharacters也无法包含另一个单引号(由于他们会终止字符串字面量)。 但是,在一个程序中混用两种引号是不推荐的(当然那也有例外的情况,比如嵌套的引号)。要解决这个问题,只需要专注使用同一种类型,也可以将quotmark选项设置为double或者single来强制你的偏好:/*jshint quotmark:single */var x =...

  • JSLint Move the invocation into the parens that contain the function

    什么时候会产生这个错误? JSLint在遇到一个立即调用函数表达式的调用括号出现在包含这个函数的括号之外时,会抛出"Move the invocation into the parens that contain the function"的错误。在下面的例子中,我们将匿名函数的返回值赋给了变量x:var x = (function () { "use strict"; return { y: 1 };})(); 为什么会产生这个错误? 这个错误是为了强调一种规范的欠缺。若是不改动之,你的代码在运行的时候并不会有任何的错误。但是,这个对这个特别的错误可以对调用括号的位置进行讨论应当引起讨论。如果你在使用JSLint,那么你就需要按照其建议的来规范。 对于移动括号的争论的目的是让代码变得更容易理解。如果括号在包含函数体外部大括号外部,就不会立即被理解为是调用整个函数。将括号移动到内部,他们就紧接着函数语句,那么就会更容易与函数相联系。 解决这个错误,可以简单的将调用的括号移动到包含函数的括号内:var x = (function...

  • JSLint Move 'var' declarations to the top of the function

    什么时候会产生这个错误? JSLint在遇到一个变量声明在了for循环的初始化语句中。以下是一个简单的计数到10的for循环语句:for (var i = 0; i 为什么会产生这个错误? 这个错误是为了强调一种可能的对语言的误解。如果你不解决这个问题,你的代码可能会和期望的一样正常工作,但是你可能误解了JavaScript是如何处理变量声明的。 所有的声明都会被提升到它们所出现的作用域的顶部。JavaScript中的变量只有函数作用域而没有块级作用域。在很多其他的语言中,变量i在上述例子中,是属于for循环体并且能够被其包含的作用域所访问到。在JavaScript中,变量实际上在所属的作用域中都能被访问到,尽管它在直到循环初始化执行的时候才被赋值。 为了解决这个问题,可以简单的将变量声明放到循环体外面。这可以让代码与其被解析的时候是一样的。var i;for (i = 0; i 更多相关的信息可以查阅相关的消息"Combine this with the previous 'var' statement".

  • JSHint Missing name in function declaration

    什么时候会产生这个错误? JSLint在遇到function关键字而它会被解析成一个紧随着一个打开的圆括号的语句时,会抛出"Missing name in function declaration"的错误。在下面例子中,我们尝试着定义一个函数,但是并没有给予它标示符。function () { "use strict";} 这个与JSLint中的 "Missing name in function statement"是等价的。更多的细节可以从该JSLint消息的页面中找到。 在JSHint1.0.0及以上版本中,你可以通过一个 特殊的选项语法来忽略该警告。这个警告的标示符是W025。这也就意味着你可以使用/*jshint -W025 */来告诉JSHint不要报告这个警告。

  • JSLint Missing name in function statement

    什么时候会产生这个错误? JSLint在遇到function关键词之后紧跟一堆括号,而此时应当被用作一个声明语句时,会抛出"Missing name in function statement"的错误。下面例子中,我们尝试着定义一个函数,但是没有给其函数名:function () { "use strict";} 为什么会产生这个错误? 这个错误是为了强调一个JavaScript语法错误。除非你更正了这个问题,否则你的代码是不会正常运行的。 ECMAScript语法描述了一个函数语句(或者说描述语句)必须有一个标示符:(ES5 §13):FunctionDeclaration :    function Identifier ( FormalParameterListopt) { FunctionBody } Notice that the Identifier part of the grammer is...

  • JSLint JSHint Do not use {a} as a constructor

    什么时候会产生这个错误? JSLint和JSHint在遇到使用new操作符操作String, Number, Boolean, Math或JSON时会抛出"Do not use {a} as a constructor"的错误。在下面的例子中,我们尝试着将一些值或变量放入这些对象中以作为构造函数调用:var str = new String("hello"), num = new Number(10), bool = new Boolean(false), math = new Math(), json = new...

  • JSLint JSHint '{a}' is not a function

    什么时候会产生这个错误? JSLint和JSHint在遇到尝试将Math对象作为函数调用的时候,会抛出"'{a}' is not a function"的错误。JSLint的这个错误是为了当其遇到将JSON对象作为函数调用时而抛出错误设计的,尽管这个功能现在并不是很常见。以下是一个例子:var x = Math(); 为什么会产生这个错误? 这个错误的抛出是为了强调对语言的误解。全局对象中的Math属性在规范中是这样被描述的(ES5 §15.8):The Math object does not have a [[Construct]] internal property; it is not possible to use the Math object...

  • JSHint '{a}' is not a statement label

    什么时候会产生这个错误? JSHint在遇到一个break或者continue语句的指向一个不存在的标签的时候会抛出"'{a}' is not a statement label"的错误。在下面的例子中,我们尝试跳出for循环到example标签。function labelExample() { "use strict"; var i; for (i = 0; i 这个JSHint的警告和JSLint中的"'{a}' is not a label"是等价的。更多的消息可以在这个JSLint的页面上找到。 在JSHint1.0.0及以上版本中,你可以通过一种特殊的选项语法来忽略之。这个警告的标示符是W090.也就意味这你可以通过/*jshint -W090 */来告诉JSHint不报告这个警告。

  • JSLint '{a}' is not a label

    什么时候会产生这个错误? JSLint在遇到一个break或者continue语句指向一个并不存在的标签时,会抛出"'{a}' is not a label"的错误。在下面这个例子中,我们尝试着跳出一个for循环到example标签:function labelExample() { "use strict"; var i; for (i = 0; i 为什么会产生这个错误? 这个错误是为了强调一个JavaScript语法错误。在循环语句中引用一个并不存在标示符是不合法的。这在ECMAScript5规范中是这样被陈述的(section §12.7 and section §12.8): A program is considered syntactically incorrect if......

  • JSHint Octal literals are not allowed in strict mode

    什么时候会产生这个错误? JSHint在strict模式中遇到一个字符串字面量包含着以'0'开头的八进制转义字符串,会抛出"Octal literals are not allowed in strict mode"。在下面这个例子中我们尝试着将一个包含着八进制的字符串赋值给变量x:(function () { "use strict"; var x = "\07";}()); 为什么会产生这个错误? 这个错误是为了强调对废弃语言特性的使用。在ECMAScript5规范中,八进制序列被废弃并且不再会被使用。如果使用之,由于这个特性已经逐渐被抛弃,而使得与现有更新的JavaScript引擎不兼容。 这个消息和JSLint中的"Don't use octal: '{a}'. Use '\\u...' instead"等价。可以查阅那个页面来获取关于其起因和该错误的解决办法的更多的相关信息。 JSHint的一个小疏忽 也许这个错误消息是为了一种不同的情况,当然也可能稍微有点误导人。上述例子是一个八进制转义序列的示例,而不是一个八进制字面量(ES5 Annex B.1.1):OctalIntegerLiteral...

  • 什么时候会产生这个错误? 当JSLint遇到一个顺带着0~7的数字转义字符字符串字面量时,会抛出"Don't use octal: '{a}'. Use '\u...' instead"的错误。在下面的例子中,我们尝试着将一个字符串赋值给变量x。八进制的转义字符\251对应着copyright的符号 ©:var x = "Copyright \251"; 为什么会产生这个错误? 这个错误是为了强调废弃语言特性的使用。根据ECMAScript5标准,八进制序列已经被废弃了并且不再会被使用。这样使用相当于冒着和最新的JavaScript引擎不兼容的危险。 最新的规范包含了以下内容(ES5 B.1):Past editions of ECMAScript have included additional syntax and semantics for specifying octal literals...

  • JSLint JSHint Unexpected parameter '{a}' in get {b} function

    什么时候会产生这个错误? 当JSLint和JShHint遇到一个属性getter函数中含有形参时,会抛出"Unexpected parameter '{a}' in get {b} function"错误。在下面的例子中,我们创建了一个对象x并包含了一个getter和setter。其中getter会返回所设置值的一半(由于getter和setter是ECMAScript5特性,我们需要设置`es5`选项,否则JSLint会忽略之):/*jslint es5: true */var x = { real: 10, get x (value) { "use strict"; return this.real / 2; }, set x (value)...

  • JSLint Expected parameter (value) in set '{a}' function

    什么时候会产生这个错误? JSLint在遇到一个属性设置函数中有多个命名的参数,而其中没有一个参数是叫做value的时候,就会抛出"Expected parameter (value) in set '{a}' function"的错误。在下面这个例子中,我们创建了一个对象x,其中包含了一个getter和setter。其中getter函数会返回一半的所设置的一半(由于getters和setters是ECMAScript5中的特性,我们需要在代码中设置es5选项,否则JSLint会忽略之。):/*jslint es5: true */var x = { real: 10, get x () { "use strict"; return this.real / 2; }, set x (val,...

  • JSLint JSHint Missing radix parameter

    什么时候会产生这个错误? 当JSLint与JSHint发现调用parseInt函数时只有一个参数,就会抛出"Missing radix parameter"的错误,请看以下例子:parseInt("10"); 为什么会产生这个错误? 这个错误是为了强调潜在的、会引发问题的错误。parseInt函数的第二个参数用来声明一个基数。如果没有声明基数,函数会返回令人感到惊奇的结果。 如果没有声明基数,并且字符串是以0开头,这个字符串就会当做一个8进制数字(以8为基数)。如,parseInt("010");会返回8,而不是10。这个行为在ECMAScript 3中是被允许的。但是,ECMAScript 5中是这么描述的: parseInt函数会根据string的内容以及指定的radix解析产生一个整数值。导致字符串中的空格被忽略。如果radix没有定义或者为0, 那么基数就会被假定为10,除非数字以0x或者0X开头,也就是将数字假定为16进制。 因此在ECMAScript 5中,这个关于parseInt的怪癖被扔掉了。但是,考虑到你需要让你的代码在老一点的、不支持ES5的环境中成功运行,你还是需要传递一个基数参数给parseInt函数。 JSLint与JSHint的一点小小的疏忽 值得一提的是,不管是JSLint还是JSHint都不会去检查第二个参数的类型。它们只会检查第一个参数。因此,以下代码不会产生任何错误,尽管它表现的和你不传递任何基数一模一样。这也是在使用parseInt函数时的值得警惕的地方:parseInt("10", null); 在JSHint 1.0.0及以上版本中你可以通过特定的选项语法来忽略这个警告。这个警告的标识符是W065,也就是说你可以通过/*jshint -W065*/让JSHint不提示这个问题。

  • JSLint JSHint Read only

    JSLint and JSHint will throw the "Read only" error when they encounter an attempt to assign a value to built-in native object. In the following example we attempt to overwrite...

  • JSHint Redefinition of '{a}'

    什么时候会产生这个错误? 若JSHint遇到声明的变量名和内建的原生对象名称相同,就会抛出"Redefinition of '{a}'"错误。在下面这个例子中我们尝试着用String这个名称来声明一个对象:var String = "My String"; 为什么会产生这个错误? 这个错误是为了强调潜在的有问题的代码。你的代码如果不修改,可能在运行时不会出现错误,但是这会让其他人感到十分困惑,特别是那些乍眼一看的人们,并且这很可能会对第三方的脚本造成影响。 当然,你完全可以重新对任意的内置函数进行赋值或者重载,但是对于这种操作是有一定限制的。JSHint完全禁止进行这种操作,并且不提供任何参数去允许之。 "只读函数"的列表如下: Array Boolean Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error eval EvalError Function hasOwnProperty isFinite isNaN JSON Map Math NaN...

  • JSHint Did you mean to return a conditional instead of an assignment?

    什么时候会产生这个错误? 当一个返回语句包含一个二义的赋值语句而不是一个比较式时,JSHint会抛出这个错误:function multiply(num_one, num_two) { return result = num_one * num_two;} 在这种情况下,JSHint并不确定你是否想要返回一个布尔值还是只是表达式的类型,或者你想要先对一个变量进行赋值并在同一行内将其返回。最简单的修复方法是很明确的:function multiply(num_one, num_two) { var result = num_one * num_two; return result;}

  • JSLint JSHint A regular expression literal can be confused with '/='

    什么时候会产生这个错误? JSLint和JSHint(版本1.0.0之前)在遇到正则表达式字面量由=符号起始时,会抛出"A regular expression literal can be confused with '/='"的错误。在下面例子中我们尝试着将正则表达式字面量"=1"赋给变量=去匹配字符串:var regex = /=1/; 为什么会产生这个错误? 这个错误是为了强调潜在的让人感到疑惑的代码。若不修复这个错误,你的代码会正常运行,但是会让其他人感到困惑,特别是那些随便扫了一眼你的脚本的人们。 /字符在JavaScript中是有歧义的。它既能表示正则表达式开始,正如上面一个例子中展示的那样,也能被解释为除号。就像大多数算术符号一样,除号可以和赋值符号一起作为组合符号:var x = 10;x /= 5; // Shorthand division-assignment operator 这个二义性并不是一个真正的问题,因为解释器总是能区分两种不同的用法。但是,你可以看到上面的正则表达式可能造成一开始的疑惑。 为了解决这个问题,你可以简单的将=符号在正则表达式中转义。这会表现的很正常,因为=符号已经不是第一个字符,所以错误不会再出现:var regex = /\=1/;...

  • JSLint JSHint Stopping. ({a}% scanned)

    什么时候会产生这个错误? JSLint在遇到一个JavaScript语法错误并且无法继续可靠的解析程序。JSHint会在相同情况下抛出这个错误,但是只有当passfail选项设置为true时。在下面这个例子中,我们给出了半个不合法的变量语句并且不能被解析为一个完整的JavaScript程序:var 为什么会产生这个错误? 这个错误是为了强调JavaScript语法错误以及解析器无法可靠的完成解析你的程序。你的代码无法继续运行,除非将这个解决了。 事实上这个取决于你的程序,但是在你的代码中另外的错误却会因为这个错误而产生。在我们的例子中,变量语句会没有标示符:var x; 在JSHint1.0.0及以上版本中你可以通过特殊的语法选项来将其忽略。但是这个错误关系到一个严重的语法错误,你不能禁用之。

  • JSHint Strings must use singlequote

    什么时候会产生这个错误? 当JSHint(从版本r09开始)遇到一个用双引号包含的字符串,并当quotmark被设置为single时。在下面例子中我们尝试着将字符串字面量赋给变量x:/*jshint quotmark:single */var x = "My String"; JSHint在相反的情况下会抛出"Strings must use doublequote"的错误,即当字符串被单引号包裹并将quotmark设置为double:/*jshint quotmark:double */var x = 'My String'; 为什么会产生这个错误? 这个错误是为了强调一种不同于一些特定编程风格的风格。在JavaScript中使用单引号和双引号是没有区别的。这在语法中关于字符串字面量已经解释的很清楚(ES5 §7.8.4):StringLiteral ::    " DoubleStringCharactersopt "    ' SingleStringCharactersopt ' 唯一不同的是DoubleStringCharacters不能包含另外的双引号以及SingleStringCharacters不能包含另外的单引号(那将会字符串字面量)。 如果设置了这个选项,那么这就像你自己的代码库,要求你能够符合那种代码规范中的引号规则。为了解决这个错误,简单的使用正确的引号类型:/*jshint quotmark:single */var...

  • JSLint JSHint ['{a}'] is better written in dot notation

    什么时候会产生这个错误? JSLint和JSHint在遇到尝试使用字符串字面量和方括号访问不是保留字的对象的属性时会抛出"['{a}'] is better written in dot notation"的错误。在以下例子中我们尝试着访问x对象的prop属性:var x = { prop: 10 }, y = x['prop']; 为什么会产生这个错误? 这个错误是为了强调不必要的或潜在的让人感到困惑的代码。在很多编程语言中,使用点来访问对象的属性是非常常见的。从语法角度而言也没有任何问题,两者都会在所有宿主环境中运行正常。但是使用点来访问属性总是可以省下三个字符的控件。以下是上述代码片段,使用点来访问的版本:var x = { prop: 10 }, y = x.prop; 但是,如果属性的名称是保留字那么就必须使用方括号来访问。JSLint和JSHint会在这时抛出错误。在下面这个例子中,x有一个叫做class的属性。注意JSLint并不会抛出错误,及时我们使用了方括号:var x...

  • JSLint Unexpected sync method: '{a}'

    什么时候会产生这个错误? JSLint在遇到一段尝试访问一个名称以"Sync"结尾的属性时会抛出"Unexpected sync method: '{a}'"的错误。在下面这个例子中,我们尝试着从一个文件夹中获取一个文件中的一个数组(Node.js为例):/*jslint node: true */var fs = require("fs"), files = fs.readdirSync("myDirectory"); 注意尽管错误消息讲的是"method",JSLint实际上抛出这个错误是因为会访问到边界区,而不管是否是一个调用方法(并不检测标示符之后的一对括号)。 为什么会产生这个错误? 这个错误是为了强调约定的缺少以及可能的错误实践。若是不修改这些代码,你的代码在运行时并不会报错,但是事实上有更好的方式去实现之。 特别是在Node.js中,有很多异步的方法提供了同步一样的功能。例如:有一个readdir方法,它是上文代码中readdirSync方法的异步版本。 在绝大多数的情况下,最好还是选择使用异步方法而不是同步的。理由很简单,同步的方法会阻塞运行直到它们完成了方法需要做的事情。在浏览器中,这会显而易见的导致"fronzen"页面。为了解决这个问题,可以简单的修改你的代码,使用异步方法:/*jslint node: true */var fs = require("fs");fs.readdir("myDirectory", function (err, files) { "use...

  • JSLint Unexpected TODO comment

    什么时候会产生这个错误? JSLint在遇到一个以"TODO"开头的注释时会抛出"Unexpected TODO comment"的错误。以下是一个例子:// TODO: Finish writing about JSLint errors 为什么会产生这个错误? 这个错误是为了强调缺乏理解。以单词"TODO"开头的注释通常用来标记未完成的代码。JSLint采取这种安全措施并假设你的代码如果包含了这样的注释是不能用于生产环境的。 如果你高兴使用"TODO"注释在你的代码中,你可以通过设置todo选项来告诉JSLint来忽略之:/*jslint todo: true */// TODO: Finish writing about JSLint errors

  • JSHint This function has too many parameters

    为什么会产生这个错误? JSHint (版本1.0.0及以上) 在遇到一个函数的形参数量比maxparams选项声明的要多时,会抛出"Too many parameters per function ({a})"的错误。以下是一个我们尝试着声明一个带有三个参数的函数的例子:/*jshint maxparams: 2 */function Person(name, age, gender) { "use strict"; this.name = name; this.age = age; this.gender = gender;} 这个与老版本的JSHint"Too many parameters...

  • JSHint Too many parameters per function ({a})

    什么时候会产生这个错误? JSHint (版本1.0.0以下) 在遇到一个函数的形参数量比maxparams选项声明的要多时,会抛出"Too many parameters per function ({a})"的错误。以下是一个我们尝试着声明一个带有三个参数的函数的例子:/*jshint maxparams: 2 */function Person(name, age, gender) { "use strict"; this.name = name; this.age = age; this.gender = gender;} 为什么会产生这个错误? 这个错误是为了强调一种代码风格上的偏差。ECMAScript规范没有声明一个函数可以接受的最小或最大参数的数量(ES5 §13):FunctionDeclaration...

  • JSLint Type confusion: {a} and {b}

    什么时候会产生这个错误? JSLint(2011年六月到七月之间的版本)在遇到尝试改变赋值给变量的数据类型时会抛出"Type confusion: {a} and {b}"的错误。以下是示例代码:var x = 1; // numberx = "str"; // string 为什么会产生这个错误? 这个错误是为了强调潜在的让人感到困惑的代码片段。当你对一个变量赋值时,一般都会期望在整个程序中获得同一个变量类型。但是在JavaScript中你可以自由修改任意变量的任意值,而不用考虑它现在的值,所以对于这个警告有很多基于语言本身的原因。 事实上,在JSLint中对这个错误的提示的生命周期是如此之短,以至于这看起来就像是一个实验,从未真正被公开使用过。不幸的是,由于一些使用了老版本的JSLitn的内置的JavaScript提示功能,类型检测仍然存在,它出现在很多IDE和编辑器中。 为了解决这个问题(假设你想要拥有对变量赋予不同类型的值的能力),你需要升级到最新版本的JSLint。如果不能升级,那么你可以设置confusion这个选项:/*jslint confusion: true */var x = 1; // numberx = "str"; //...

  • JSLint JSHint Unclosed string

    什么时候会产生这个错误? JSLint和JSHint在遇到一个字符串没有在换行或者在程序末尾时闭合,会抛出"Unclosed string"的错误。在这第一个例子中,不小心忘了将字符串闭合:var myString = "my string, myNumber = 10; 在下一个例子中,我们希望字符串包含一个反斜杠。所以字符串看起来像是闭合的但事实上没有。原因是引号被转义了:var myString = "my string\", myNumber = 10; 最后一个例子,在支持ECMAScript 5的环境下,字符串在程序的最后没有闭合(之前的两个例子出错是因为没有在行末闭合):/*jslint es5: true */var myString = "my multiline \ string 为什么会产生这个错误?...

  • JSLint JSHint Unclosed comment

    什么时候会产生这个错误? JSLint和JSHint在遇到多行注释没有用*/结尾,会抛出"Unclosed comment"的错误。以下是一个例子:/* This is a comment * but I forgot to * close it. 为什么会产生这个错误? 这个错误是为了强调一个JavaScript语法错误。ECMAScript5规范列出了以下关于多行注释的语法 (section §7.4): MultiLineComment ::     /* MultiLineCommentCharsopt */ 我们可以从上述引用内容中中了解到多行注释必须以*/字符结尾。如果你有一个没有闭合的多行注释,那么解释器在到文件末端时就会抛出一个语法错误。以下仍是上述代码片段,但是这次我们将多行注释闭合了:/* This is a comment *...

  • JSLint JSHint Unclosed regular expression

    什么时候会产生这个错误? 当JSLint和JSHint在遇到一个正则表达式字面量没有/闭合字符时,会抛出"Unclosed regular expression"的错误。以下是一个例子:var r = /^unclosed$; 为什么会产生这个错误? 这个错误是为了强调严重的语法错误。语言规范展示了正则表达式字面量的语法(ES5 §7.8.5):RegularExpressionLiteral ::    / RegularExpressionBody / RegularExpressionFlags 这份规范很清楚的解释了正则表达式字面量必须以/字符结尾。如果不这样做会产生一个语法错误。解决这个问题,只需闭合问题中的正则表达式:var r = /^unclosed$/; 在JSHint1.0.0及以上版本中你可以通过特殊的语法来忽略任意警告。但是这个消息和严重的语法错误有关,所以你无法忽略之。

  • JSLint Unexpected 'with'

    什么时候会产生这个错误? JSLint(自2013年七月)在遇到使用with语句时,会抛出"Unexpected 'with'"的错误。而在2013年七月之前,这个消息是"Expected an identifier and instead saw 'with'", 尽管原因是一样的。以下是一个例子:function example() { var a = { b: 10 }, c = 20; with (a) { b = 30; c =...

  • JSLint Unexpected '++'

    什么时候会产生这个错误? JSLint在遇到使用自增操作符++的时候会抛出"Unexpected '++'"的错误。当然也会在遇到使用自减操作符--的时候抛出"Unexpected '--'"的错误。以下是一个例子:var x = 1, y = 10;x++;y--; 为什么会产生这个错误? 这个错误消息可能是所有JSLint错误消息中争论最大的一个了。它的存在正是为了警告你JSLint遇到了违反特定的代码风格,特别的来说,是JSLint作者Douglas Crockford的风格。要知道他的理由,可以阅读JSLint的文档:The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive...

  • JSLint Unexpected assignment expression

    什么时候会产生这个错误? 当一个包含不明确地赋值操作符存在于,应当为比较操作符表达式的while,for或者if语句中,JSLint(自2013年七月)会抛出这个错误。这个错误是原来"Expected a conditional expression and instead saw an assignment"JSLint与JSHint消息的新版:var x;if (x = 0) { var b = 1;}

  • JSLint JSHint Unexpected '{a}'

    什么时候会产生这个错误? JSLint和JSHint在很多情况下会抛出"Unexpected '{a}'"错误,这些情况包含了严重的语法错误以及简单的代码样式问题。在此分类下的一些常见的问题可以在以下这个列表中看到: Unexpected '++' Unexpected '--' Unexpected 'NaN' Unexpected 'with'

  • JSLint Unexpected comment

    什么时候会产生这个错误? 当JSLint遇到一个单行或者多行JSON文件中的注释时,会抛出"Unexpected comment"的错误。这个错误只会在JSON模式下产生(当第一个输入字符为{或者[时进入JSON模式)。在以下例子中我们尝试着给我们的JSON属性进行注释:{ "name": "James", // My first name "age": 23} 为什么会产生这个错误? 这个错误是为了强调一个致命的语法错误。JSON 规范没有提供任何机制用于注释。尝试解析上述例子(使用JSON.parse)时会抛出语法错误。解决这个问题,可以将所有的注释从JSON文件中移除:{ "name": "James", "age": 23}

  • JSLint Unexpected 'else' after 'return'

    什么时候会产生这个错误? 当JSLint(2013年7月前)遇到一个紧跟着包含return语句的if代码块之后的else代码块。以下是示例代码:function example(x) { "use strict"; if (x) { return 1; } else { return 2; }} 为什么会产生这个错误? 这个错误用来强调一段完全没有用处的代码。如果代码执行到if代码块中,函数会返回。这样没有可能会进入到else代码块中。因此,你可以简单的将else代码块去掉并将其内容放到if代码块之后。以下是没有错误的上述代码片段版本:function example(x) { "use strict"; if (x) { return 1; } return...

  • JSLint Unexpected label '{a}'

    什么时候会产生这个错误? 当JSLint遇到一个标签语句声明内的语句不是迭代或者switch语句或该标签语句在全局执行上下文中。以下这个例子包含了一个标签的var语句:function test() { "use strict";labelled: //Label for the following var statement var x = 10;} 为什么会产生这个错误? 这个错误出现于两个不同的场景。两者都需要各自的解释。 一个标签语句内的语句不是迭代或switch语句 这个错误在这种情况下被抛出,是因为JSLint发现了一段毫无用处并可能让人感到困惑的代码。根据ECMAScript标准,任何语句都是可以被用作标签的。但是,由于语言本身没有定义goto语句,标签语句只有在与循环语句或者switch语句中的break和continue语句相关联的时候才有用。 规范总结了上面这一点在section §12.12中: A Statement may be prefixed by a label....

  • JSHint Unnecessary semicolon

    什么时候会产生这个错误? JSHint在遇到一个代码块或者函数声明后紧跟着一个分号时会抛出"Unnecessary semicolon"的错误。在下面这个例子中我们错误的将一个分号置于了if语句体以及另一个在函数声明的末尾:function example(a) { if (a) { return true; };}; 为什么会产生这个错误? 这个错误是为了强调那些毫无意义的代码。代码语句块或函数声明后的分号不是必须的。规范很清晰的告诉了我们分号在哪里是必须的。例如,对比变量声明语句(ES5 §12.2)和代码块的语法(ES5 §12.1):Block :    { StatementListopt }VariableStatement :    var VariableDeclarationList ; 代码块语句的语法表示最后闭合的大括号后是不需要分号的。这对于函数声明也是一样的。你可以将那些不必要的分号去除来解决这个错误:function example(a) { if (a) { return true; }}...

  • JSLint JSHint It is not necessary to initialize '{a}' to 'undefined'

    什么时候会产生这个错误? JSLint和JSHint在遇到一条将变量赋值为undefined的赋值语句时,会抛出"It is not necessary to initialize '{a}' to 'undefined'"的错误。以下是我们尝试着声明x并将undefined赋值给它的例子:var x = undefined; 为什么会产生这个错误? 这个错误是为了强调一些令人感到疑惑的代码。如果不修改,这些代码会正常运行且没有错误,但是你会不必要的增加了脚本的大小。 由于变量声明在其出现的作用域内置于最顶部,且赋值操作在正常的位置尽心,变量总是会被隐式地初始化为undefined。以下是当你进入一个作用域时发生的一切:(ES5 §10.5):8. For each VariableDeclaration... d in source text order do    a. Let dn be the...

  • JSLint Unnecessary 'else' after disruption

    什么时候会产生这个错误? 这是老版本的JSLint中"Unexpected 'else' after 'return'"警告的新的升级版本。更多的内容可以在那个消息的内容中找到。 JSLint (自2013年七月来)在遇到一个跟随在if代码块中包含像return或者throw那样的终止运行的语句的else语句块时,会抛出"Unnecessary 'else' after disruption"错误。以下是一个例子:function example(x) { "use strict"; if (!x) { throw "A throw is disruptive"; } else { return true; }} 为什么会产生这个错误? 这个错误被抛出用来强调一段毫无意义的代码片段。如果代码运行到if代码块内时,运行过程就会中断(可能是return了,或者抛出了一个错误)。所以代码就不会运行到else代码块中了。因此,你可以简单的将else去掉,并将其中的内容直接放到if代码块后。以下仍然是上述代码片段,但是没有错误:function...

  • JSLint Unnecessary use strict

    什么时候会产生这个错误? JSLint在遇到"use strict"语句出现在一段已经运行在严格模式下的代码中时,会抛出"Unnecessary use strict"的错误。在下面这个例子中,我们将一个"use strict"语句放在全局作用域中,另一个放在函数中:function Example() { //This directive applies to all contained scopes "use strict"; this.method = function () { "use strict"; };} 为什么会产生这个错误? 抛出这个错误是为了强调完全没有目的的代码片段。"use strict"语句在其出现的作用域中起作用,当然也包括其后裔的可执行上下文。以下是ECMAScript 5规范中关于严格模式和函数的内容:(section §10.1.1):...

  • JSHint The array literal notation [] is preferrable

    什么时候会产生这个错误? JSLint和JSHint(1.0.0版本前)遇到使用new操作符调用Array构造函数时,会抛出"The array literal notation [] is preferrable"错误。下面是一个例子:var x = new Array(); 这和JSLint以及老版本的JSHint中的"Use the array literal notation []"的警告是一样的。更多的内容可以在该页面中找到。 在JSHint 1.0.0及以上版本中你可以通过一种特殊的语法选项special option syntax来忽略任意警告。这个警告的标示符是W009。也就是说你可以通过/*jshint -W009 */让JSHint不报出这个警告信息。

  • JSLint JSHint Use the array literal notation []

    什么时候会产生这个错误? JSLint和JSHint(在版本1.0.0之前)在遇到使用new操作符调用Array构造函数时,会抛出"Use the array literal notation []"的错误。下面是一个例子:var x = new Array(); 为什么会产生这个错误? 这个错误是为了强调潜在的危险以及不必要的、啰嗦的代码片段。在我们了解为什么上述代码会有潜在危险之前,下面是一个使用数组字面量重写的可以通过JSLint和JSHint检测的版本。注意,它显然更短:var x = []; 由于Array构造器事实上只是全局对象的一个属性,它可以被重写。如果它被重写了的话,那么第一个例子就有可能会产生一个类型错误。例如,如果你运行了一些像Array = 50这样的代码,就会抛出一个类型错误,因为Array不再是一个函数了。 下面是一个将Array构造函数重写的例子。注意JSLint和JSHint都不知道发生了什么。因此,这是一个安全使用的方法并且完全禁用了Array构造函数:window.Array = 50;var x = new Array(); //TypeError: Array is not...

  • JSLint JSHint Spaces are hard to count. Use {a}

    什么时候会产生这个错误? JSLint和JSHint(1.0.0版本前)在遇到一个正则表达式包含了多个连续的空格字符时会抛出"Spaces are hard to count. Use {a}"的错误。在下面这个例子中我们定义了一个正则表达式,用以匹配字符串"three   spaces" (在两个单词之间有三个空格):var regex = /three spaces/; 为什么会产生这个错误? 抛出这个错误是为了强调潜在的令人疑惑的代码片段。若不改变之,你的代码可以正常运行,但是这会让其他的开发者对此感到疑惑,特别是第一眼看到它的时候。 任何想要看看这个正则表达式到底要匹配的内容时,都会去数一数有多少个空格。但这显然是不必要的,因为有一种可选的语法能让这种正则表达式变得更清晰。为了解决这个错误,可以简单的使用重复操作符:var regex = /three {3}spaces/; 或者,你可以将你的代码修改为使用RegExp的构造函数而不是正则表达式字面量,这是因为JSLint和JSHint只会在使用一个字面量定义一个正则表达式的时候抛出错误:var regex = new RegExp("three spaces");

  • JSHint The object literal notation {} is preferrable

    什么时候会产生这个错误? JSLint和JSHint(1.0.0版本前)遇到使用new操作符调用Object构造函数时,会抛出"The object literal notation {} is preferrable"错误。下面是一个例子:var x = new Object(); 这和JSLint以及老版本的JSHint中的"Use the object literal notation {}"的警告是一样的。更多的内容可以在该页面中找到。 在JSHint 1.0.0及以上版本中你可以通过一种特殊的语法选项special option syntax来忽略任意警告。这个警告的标示符是W010。也就是说你可以通过/*jshint -W010 */让JSHint不报出这个警告信息。

  • JSLint JSHint Use the object literal notation {}

    什么时候会产生这个错误? JSLint和JSHint(1.0.0版本前)遇到使用new操作符调用Object构造函数时,会抛出"Use the object literal notation {}"错误。下面是一个例子:var x = new Object(); 为什么会产生这个错误? 抛出这个错误是为了指出那些不必要的啰嗦和潜在的令人感到困惑的代码。在我们去了解为什么上述代码片段会有潜在危险的之前,这里是一个能通过JSLint和JSHint检测使用数组字面量的重写的版本。注意它显然更短:var x = {}; 由于Object构造函数事实上只是全局对象的一个属性,它可以被重写。如果它被重写了的话,那么对于第一个例子而言就可能会产生一个类型错误。例如,如果你运行了一些像Object = 50这样的代码,就会抛出一个类型错误,因为Object不再是一个函数了。 下面是一个将Object构造函数重写的例子。注意JSLint和JSHint并不知道发生了什么事。因此,这是一个安全使用的方法并且完全禁用了Object构造函数:window.Object = 50;var x = new Object(); //TypeError: Object is not...

  • JSLint Use the || operator

    什么时候会产生这个错误? 当JSLint遇到一个逻辑表达式和第一个参数相等的条件操作符时,会抛出"Use the || operator"的错误。在下面的例子中,我们尝试着一个根据a的指赋值给b:var a = false, b = a ? a : "Example"; 为什么会产生这个错误? JSLint抛出这个错误是为了指出那些不必要的啰嗦和潜在的令人感到困惑的代码。上述示例中构造的表达式可能会被用作来设置默认值,一个以函数参数为例:function example(a, b) { "use strict"; a = a ? a : "Default"; b...

  • JSLint Use a named parameter

    什么时候会产生这个错误? 当JSLint遇到尝试着用数字下标访问arguments对象的属性时,会抛出"Use a named parameter"错误。在下面这个例子中,我们将第一个函数参数赋值为x:function add2() { "use strict"; return arguments[0] + arguments[1];} 为什么会产生这个错误? 这个错误是为了强调潜在的会造成速度缓慢和令人迷惑的代码片段。使用arguments对象是缓慢的。除非你在函数内访问它,很多JavaScript引擎事实上不会创建这个对象。在Chrome 30.0中的跑分显示,使用命名的形参效率会提高54%: 除了性能上的问题,使用arguments对象而不是已命名的参数会破坏你的代码的可读性。(使用参数)会更容易理解你的函数要做什么,以及需要传递什么参数。解决这个问题,可以简单的给函数的形参列表中添加自己命名的参数:// This is much easier to understandfunction add2(firstNumber, secondNumber) { "use strict"; return firstNumber...

  • JSHint '{a}' is not defined

    什么时候会产生这个错误? 当JSHint遇到一个被引用的标示符未在var或者function语句中被声明时,会抛出"'{a}' is not defined"的错误,而且undef选项被设置为true。 在下面的代码中,我们尝试着给一个未声明的变量x 赋值,并且尝试着alert出该变量的值。同时也尝试着将一个未被声明的函数的返回值设置给变量y:/*jshint undef: true */var y = someFunction();x = 10;alert(x); 这个和JSLint中的"'{a}' was used before it was defined"警告是等价的。更多的信息可以在该消息的页面中了解。 This is the equivalent of the JSLint "'{a}'...

  • JSLint '{a}' was used before it was defined

    什么时候会产生这个错误? 当JSLint在遇到一个标示符不是var或者function声明的一部分时会抛出"'{a}' was used before it was defined"的错误。一些非常常见的该类错误的例子是引用了原生的DOM对象: "'document' was used before it was defined" "'window' was used before it was defined" "'alert' was used before it was defined" "'console'...

  • JSLint JSHint Variable {a} was not declared correctly

    什么时候会产生这个错误? JSLint和JSHint (2.1.4版本之前)在遇到一个多于一个的行内等式时会抛出"Variable {a} was not declared correctly"的错误。在这个例子中,我们尝试着将一个字符串变量赋给变量x, y and z:function example() { var x = y = z = "example";}example(); 为什么会产生这个错误? 提出这个错误是为了强调一种可能的、缺少对语言理解。一种相对常见的初学者错误就是用以上代码来同时声明并将一个值赋给多个变量。但是,上述代码实际上合下面的这段代码是一模一样的:function example() { z = "example"; y =...

  • JSLint Weird assignment

    什么时候会产生这个错误? 当JSLint遇到一个等号左右两侧的表达式“相似”时,会抛出"Weird assignment"错误。在下面这个例子中我们声明了一个变量x并随后尝试将其赋值给自己:var x = 10;x = x; 为什么会产生这个错误? 这个错误是为了强调潜在的、可能会让人感到困惑的以及一些无意义的代码。事实上,几乎没有一种情景是需要将变量(或其他内容)与之本身相对比的。

  • JSLint Weird relation

    什么时候会产生这个错误? 当JSLint遇到一个左右两侧非常“类似”的对比或者表达式两边一个是数字而另一个是字符串时,会抛出“Weird relation”的错误。在下面的例子中我们尝试着比较x和其本身:var x = 10;if (x === x) { x = 20;} 为什么会产生这个错误? 这个错误用来强调潜在的、可能会让人感到困惑的以及一些无意义的代码。事实上,几乎没有一种情景是需要将变量(或其他内容)与之本身相对比的。 但是对这种比较方式,还是有一种用例可以适用的。由于NaN这个特殊的Number值永远和其自身不相等,你可以用一种自我比较的方式来检测是否有值等于NaN。以下这个例子中,除非值为NaN,都会返回true。var x = parseInt("x", 10); //结果是 NaNif (x !== x) { /* We will only...

  • JSHint Don't use 'with'

    什么时候会产生这个错误? 当JSHint遇到一段不在strict模式下运行、但却使用了with语句的代码时就会抛出"Don't use 'with'"的错误。以下是一个例子:function example() { var a = { b: 10 }, c = 20; with (a) { b = 30; c = 40; }} 为什么会产生这个错误? 这个等同于JSLint中的"Unexpected 'with'"警告。更多的细节可以在那个提示消息的页面中找到。 在JSHint...

  • JSHint 'with' is not allowed in strict mode

    什么时候会产生这个错误? 当JSHint遇到一个一个在strict模式下运行的包含with语句的代码时,就会抛出"'with' is not allowed in strict mode"的错误。在下面这个例子中,我们尝试着在一个声明了strict模式的函数体内使用with语句:function example() { "use strict"; var a = { b: 10 }; with (a) { b = 20; }} 为什么会产生这个错误? 提示这个错误是为了强调一个JavaScript语法错误。如果不解决这个问题,你的代码就会出错。在ECMAScript 5规范清晰地描述了在strict模式下with语句的存在是非法的。 (ES5...

  • JSLint JSHint Wrap an immediate function invocation in parentheses

    什么时候会产生这个错误? 当发现一个未被括号包含的、即时执行的函数时,JSLint会抛出这个"Wrap an immediate function invocation in parentheses"错误。在某些情况下JSHint会抛出这个错误,但是只有设置了 immed 选项为true时。在下面这个例子中我们将匿名函数返回值赋给了变量x:/*jshint immed: true */var x = function () { "use strict"; return { y: 1 };}(); 为什么会产生这个错误? 产生这个错误的原因是约定的缺失。如果不修复这个错误,你的代码仍然能正常的跑起来,但是却会让他人感到困惑。 既然函数语句不能被立即执行,而函数表达式所以,一种通用的用以创建一个立即调用函数表达式的方法是简单的将一个函数语句包裹在括号内。这个打开的括号让其内的函数被解析为一个表达式,而不是一个声明语句:var x;(function ()...

  • JSHint You might be leaking a variable ({a}) here

    什么时候会产生这个错误? 当产生超过一个赋值表达式时,JSHint (版本 2.1.4及以上) 会抛出 "You might be leaking a variable ({a}) here" 的错误。在下面这个例子中,我们尝试着将一个字符串字面量赋给变量 x, y and z:function example() { var x = y = z = "example";}example(); 为什么会产生这个错误? 这个警告消息是原有JSLint和JSHint中"Variable...

Sorry, but we couldn't find any JSLint or JSHint errors that match your search.

Tip: if your search contains variable or function identifiers from your own code, try removing them. All of our article titles are generic and use placeholders instead of the identifiers that JSLint will actually include in your error message.