JSHint Options 翻译
Enforcing options
When set to true, these options will make JSHint produce more warnings about your code.
This option prohibits the use of bitwise operators such as ^
(XOR), |
(OR) and others. Bitwise operators are very rare in JavaScript programs and quite often &
is simply a mistyped &&
.
【此选项禁止使用位运算符比如^(异或),|(或)等。位运算符是非常罕见的JavaScript程序,并经常与仅仅是一个&& 和& 】
This option allows you to force all variable names to use either camelCase style or UPPER_CASE with underscores.
【此选项允许您强制所有变量名以使用驼峰式或UPPER_CASE带下划线。】
This option requires you to always put curly braces around blocks in loops and conditionals. JavaScript allows you to omit curly braces when the block consists of only one statement, for example:
while (day)
shuffle();
However, in some circumstances, it can lead to bugs (you'd think that sleep()
is a part of the loop while in reality it is not):
while (day)
shuffle();
sleep();
【此选项要求你总是把大括号各地块的循环和条件。 JavaScript允许你省略花括号当块只包含一个语句】
This options prohibits the use of ==
and !=
in favor of ===
and !==
. The former try to coerce values before comparing them which can lead to some unexpected results. The latter don't do any coercion so they are generally safer. If you would like to learn more about type coercion in JavaScript, we recommend Truth, Equality and JavaScript by Angus Croll.
【这个选项禁止使用==和!=赞成===和!==。前者试图比较它们可能导致一些意想不到的结果之前,强制值。后者没有做任何强迫因此它们通常更安全】
This option tells JSHint that your code needs to adhere to ECMAScript 3 specification. Use this option if you need your program to be executable in older browsers—such as Internet Explorer 6/7/8/9—and other legacy JavaScript environments.
【这个选项告诉JSHint你的代码需要遵循的ECMAScript3规范。如果你需要你的程序必须是可执行的在旧的浏览器,如Internet Explorer6/7/8/9-and其他传统的JavaScript环境中使用此选项。】
This option requires all for in
loops to filter object's items. The for in statement allows for looping through the names of all of the properties of an object including those inherited throught the prototype chain. This behavior can lead to unexpected items in your object so it is generally safer to always filter inherited properties out as shown in the example:
for (key in obj) {
if (obj.hasOwnProperty(key)) {
// We are sure that obj[key] belongs to the object and was not inherited.
}
}
For more in-depth understanding of for in
loops in JavaScript, read Exploring JavaScript for-in loops by Angus Croll.
This options prohibits overwriting prototypes of native objects such as Array
, Date
and so on.
/* jshint freeze:true */
Array.prototype.count = function (value) { return 4; };
// -> Warning: Extending prototype of native object: 'Array'.
【这个选项禁止覆盖原生的对象,如数组,日期等的原型。】
This option prohibits the use of immediate function invocations without wrapping them in parentheses. Wrapping parentheses assists readers of your code in understanding that the expression is the result of a function, and not the function itself.
【这个选项立即禁止使用函数调用没有包装在括号中。包装括号帮助读者理解你的代码的,表达式是一个函数的结果,而不是函数本身。
感觉是立即执行函数和函数表达式相关的】
This option enforces specific tab width for your code. For example, the following code will trigger a warning on line 4:
/*jshint indent:4 */
if (cond) {
doSomething(); // We used only two spaces for indentation here
}
【此选项强制特定的标签宽度为您的代码】
This option prohibits the use of a variable before it was defined. JavaScript has function scope only and, in addition to that, all variables are always moved—or hoisted— to the top of the function. This behavior can lead to some very nasty bugs and that's why it is safer to always use variable only after they have been explicitly defined.
Setting this option to "nofunc" will allow function declarations to be ignored.
For more in-depth understanding of scoping and hoisting in JavaScript, read JavaScript Scoping and Hoisting by Ben Cherry.
【
此选项禁止使用的变量定义它之前使用它。 JavaScript有函数作用域只,而在除此之外,所有的变量总是移动或悬挂到该函数的顶部。这种行为可能会导致一些非常讨厌的错误,这就是为什么它是安全的,他们已经被明确定义后,才总是使用变量。
将此选项设置为“nofunc”将允许函数声明被忽略。
为了更深入的了解范围界定及吊装在JavaScript中,读取JavaScript的作用域和由Ben樱桃吊装。
】
This option requires you to capitalize names of constructor functions. Capitalizing functions that are intended to be used with new
operator is just a convention that helps programmers to visually distinguish constructor functions from other types of functions to help spot mistakes when using this
.
Not doing so won't break your code in any browsers or environments but it will be a bit harder to figure out—by reading the code—if the function was supposed to be used with or without new. And this is important because when the function that was intended to be used with new
is used without it, this
will point to the global object instead of a new object.
【
此选项需要你大些的构造函数的名称。凭借此举的目的是要与新的运营商使用的功能仅仅是一个惯例,可以帮助程序员从视觉上分辨构造函数与其他类型的函数时使用它来帮助发现错误。
】
This option prohibits the use of arguments.caller
and arguments.callee
. Both.caller
and .callee
make quite a few optimizations impossible so they were deprecated in future versions of JavaScript. In fact, ECMAScript 5 forbids the use of arguments.callee
in strict mode.
【此选项禁止使用arguments.caller和arguments.callee的的。两者都是。来电和。被叫做了不少优化,不可能让他们被抛弃在JavaScript的未来版本。事实上,ECMAScript 5标准不允许在严格模式下使用arguments.callee的。】
This option warns when you have an empty block in your code. JSLint was originally warning for all empty blocks and we simply made it optional. There were no studies reporting that empty blocks in JavaScript break your code in any way.
【此选项警告,当你有一个空的块中的代码。 JSLint的最初警告所有的空块,我们只是做了可选的】
This option warns about "non-breaking whitespace" characters. These characters can be entered with option-space on Mac computers and have a potential of breaking non-UTF8 web pages.
【此选项警告有关“非中断空格”字符。这些字符可以用在Mac电脑上的选项,空格中,并有打破非UTF8网页的潜力。】
This option prohibits the use of constructor functions for side-effects. Some people like to call constructor functions without assigning its result to any variable:
new MyConstructor();
There is no advantage in this approach over simply calling MyConstructor
since the object that the operator new
creates isn't used anywhere so you should generally avoid constructors like this one.
【禁止 空构造函数 此选项禁止使用构造函数的副作用。有些人喜欢调用构造函数,而不指定其结果的任何变量:】
This option prohibits the use of unary increment and decrement operators. Some people think that ++
and --
reduces the quality of their coding styles and there are programming languages—such as Python—that go completely without these operators.
【此选项禁止使用一元递增和递减运算符。有人认为,+ +和 - 减少了他们的编码风格的品质和有编程语言,如Python,那去完全没有这些运营商。】
This option enforces the consistency of quotation marks used throughout your code. It accepts three values: true
if you don't want to enforce one particular style but want some consistency,"single"
if you want to allow only single quotes and "double"
if you want to allow only double quotes.
【这个选项强制在整个代码中使用引号的一致性。它接受三个值:true,如果你不希望强制一个特定的风格,但要一定的连贯性,“单”,如果你想只允许单引号和“双”,如果你想只允许双引号。】
This option prohibits the use of explicitly undeclared variables. This option is very useful for spotting leaking and mistyped variables.
/*jshint undef:true */
function test() {
var myVar = 'Hello, World';
console.log(myvar); // Oops, typoed here. JSHint with undef will complain
}
If your variable is defined in another file, you can use /*global ... */
directive to tell JSHint about it.
【此选项禁止使用未显示声明的变量。此选项为察觉漏水和拼写错误的变量非常有用。】
This option warns when you define and never use your variables. It is very useful for general code cleanup, especially when used in addition to undef
.
/*jshint unused:true */
function test(a, b) {
var c, d = 2;
return a + d;
}
test(1, 2);
// Line 3: 'b' was defined but never used.
// Line 4: 'c' was defined but never used.
In addition to that, this option will warn you about unused global variables declared via/*global ... */
directive.
This can be set to vars
to only check for variables, not function parameters, or strict
to check all variables and parameters. The default (true) behavior is to allow unused parameters that are followed by a used parameter.
【此选项警告,当你定义和从不使用的变量。它是用于一般的代码清理非常有用的】
This option requires all functions to run in ECMAScript 5's strict mode. Strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode eliminates some JavaScript pitfalls that didn't cause errors by changing them to produce errors. It also fixes mistakes that made it difficult for the JavaScript engines to perform certain optimizations.
Note: This option enables strict mode for function scope only. It prohibits the global scoped strict mode because it might break third-party widgets on your page. If you really want to use global strict mode, see the globalstrict option.
【
此选项需要的所有功能,ECMAScript 5中的严格模式下运行。严格模式是一种选择加入JavaScript的一个变种限制。严格模式消除了一些JavaScript的缺陷是没有改变他们产生错误导致错误。它还修正错误,使得它难以对JavaScript引擎来执行某些优化。
注意:这个选项开启严格模式唯一的功能范围。它禁止在全球范围的严格模式,因为它可能会破坏第三方小部件您的网页上。如果你真的想使用全局严格模式,请参阅globalstrict选项。
】
This option makes it an error to leave a trailing whitespace in your code. Trailing whitespaces can be source of nasty bugs with multi-line strings in JavaScript:
// This otherwise perfectly valid string will error if
// there is a whitespace after \
var str = "Hello \
World";
【通过该选项,错误的留在你的代码中的尾随空格。尾部空格可以是讨厌的错误在JavaScript中使用多行字符串来源:】
This option lets you set the max number of formal parameters allowed per function:
/*jshint maxparams:3 */
function login(request, onSuccess) {
// ...
}
// JSHint: Too many parameters per function (4).
function logout(request, isManual, whereAmI, onSuccess) {
// ...
}
【此选项允许您设置的每个函数允许的形式参数的最大数量】
This option lets you control how nested do you want your blocks to be:
/*jshint maxdepth:2 */
function main(meaning) {
var day = true;
if (meaning === 42) {
while (day) {
shuffle();
if (tired) { // JSHint: Blocks are nested too deeply (3).
sleep();
}
}
}
}
【此选项可让您控制如何嵌套的数量,你希望你的模块是:】
This option lets you set the max number of statements allowed per function:
/*jshint maxstatements:4 */
function main() {
var i = 0;
var j = 0;
// Function declarations count as one statement. Their bodies
// don't get taken into account for the outer function.
function inner() {
var i2 = 1;
var j2 = 1;
return i2 + j2;
}
j = i + j;
return j; // JSHint: Too many statements per function. (5)
}
【此选项允许您设置每个功能允许报表的最大数量:】
This option lets you control cyclomatic complexity throughout your code. Cyclomatic complexity measures the number of linearly independent paths through a program's source code. Read more about cyclomatic complexity on Wikipedia.
【这是什么意思 控制复杂度的?
此选项可让您在整个代码控制圈复杂度。圈复杂度通过一个程序的源代码测量线性独立路径数。了解更多关于圈维基百科上的复杂性。】
This option lets you set the maximum length of a line.
【此选项允许您设置行的最大长度】
Environments
These options let JSHint know about some pre-defined global variables.
This option defines globals exposed by modern browsers: all the way from good old document
andnavigator
to the HTML5 FileReader
and other new developments in the browser world.
【支持代码运行的在浏览器上,包含相关的api】
Note: This option doesn't expose variables like alert
or console
. See option devel
for more information.
This option defines globals exposed by CouchDB. CouchDB is a document-oriented database that can be queried and indexed in a MapReduce fashion using JavaScript.
【此选项定义了CouchDB的暴露全局。 CouchDB是可以查询的,并在使用JavaScript MapReduce的方式进行索引的面向文档的数据库。】
This option defines globals that are usually used for logging poor-man's debugging: console
,alert
, etc. It is usually a good idea to not ship them in production because, for example,console.log
breaks in legacy versions of Internet Explorer.
【开发和调试用到的api,此选项定义全局变量,通常是用于登录穷人的调试:控制台,提醒等,这通常是一个好主意,不发货他们在生产,因为,例如,在Internet Explorer中的旧版本的console.log休息。】
This option defines globals exposed by the Dojo Toolkit.
This option defines globals exposed by the jQuery JavaScript library.
This option defines globals exposed by the MooTools JavaScript framework.
This option defines globals available when your code is running inside of the Node runtime environment. Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model. This option also skips some warnings that make sense in the browser environments but don't make sense in Node such as file-level use strict
pragmas and console.log
statements.
This option defines non-standard but widely adopted globals such as escape
and unescape
.
This option defines globals available when your core is running inside of the PhantomJS runtime environment. PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
This option defines globals exposed by the Prototype JavaScript framework.
This option defines globals available when your code is running inside of the Rhino runtime environment. Rhino is an open-source implementation of JavaScript written entirely in Java.
This option defines globals available when your code is running inside of a Web Worker. Web Workers provide a simple means for web content to run scripts in background threads.
This option defines globals available when your code is running as a script for the Windows Script Host.
This option defines globals exposed by the YUI JavaScript framework.