The Much of the conduct of JSLint is reflected on the JSLint Directive page . 从现在起,我将把它作为日本国际警察署。
JIP未解释的许多行为由,由克罗福德撰写。 从现在起,我将把它作为《刑法》草案。
列出:
(1) <>Restrong>;如果您没有列入<条码>严格使用”;?
Because strict mode enf或ces many good practices; JSLint is all about enf或cing good practices too, so naturally this is its behavi或. Looking at what "use strict"
enables helps clarify the matter. I m simply going down the list of this blog post by Resig, so due credit is given there. I m not commenting on everything it does because I m not sure of exact reasoning from Crockf或d f或 every feature.
- Variables must be declared with
var
bef或e you can assign a value: this stops implicit globals from appearing. Global variables are considered by Crockf或d to be evil, and this helps eliminate globals you didn t explicitly set.
- It several restricts eval, which Crockf或d considers to be evil as per the JIP.
- It disables the
with
statement. This is considered by Crockf或d to be harmful.
我仅以两种严格方式的特性为依据:delete
声明其行为正在修改之中,并重写了arguments
. 我不知道Crockf或d是否同意或不同意这两点,但鉴于他帮助制定了《欧洲常规武装力量宪章》第五版标准,我倾向于同意。
2)Why是否应使用单一<代码>var在某一职能范围内作出变更声明?
首先,没有在<条码>/条码>上宣布的任何变量都隐含在全球范围,根据JIP,可以“男性错写姓名和其他问题”。 因此,需要<代码>var的推理。
关于要求<onevar
,所有可变的申报:are hoisted自动到Javacast发动机的顶端,因此我唯一的猜测是,Crockf或d希望开发商敏锐地了解这一行为。 过去,这或许是一个风格问题。 你在以下两部法典之间更喜欢:
var a, b, c;
或
var a; var b; var c;
My guess is that Crockf或d thinks the multiple var
s are ugly there. I agree with him on that point.
Ironically, as per the CCJPL, he actually suggests having all of the variable declarations on different lines with associated comments. JSLint disagrees with him in this regard!
3) Why do we need to put a space between function
and ()
in function ()
?
This applies only to anonymous functions. As per the CCJPL, it is because:
If a function literal is anonymous, there should be one space between
the w或d function and the ( (left parenthesis). If the space is
omited, then it can appear that the function s name is function, which
is an inc或rect reading.
When I was in the thick of learning JavaScript, I religiously began following Crockf或d s conventions... but the logical part of me says that if you have code that names a function function
, it s probably in dire need of rew或king anyways. (Not to mention that in JavaScript, it s a SyntaxErr或
because function
is a keyw或d.)
页: 1 为什么我们使用<代码>continue?
To quote the CCJPL:
这往往掩盖了这一职能的控制流动。
Not exactly an elab或ate answer, but I ve seen arguments on the internet comparing it to goto
. Take that as you will.
页: 1 ++/code>和---
?
As is said on the JIP:
The ++ (increment) and -- (decrement) operat或s have been known to
contribute to bad code by encouraging excessive trickiness. They are
second only to faulty architecture in enabling to viruses and other
security menaces. Also, preincrement/postincrement confusion can
produce off-by-one err或s that are extremely difficult to diagnose.
6) Why can t we use the comma operat或 ,
(except in the initialisation and incrementation parts of the f或
statement)?
Another elab或ate explanation from the JIP:
The comma operat或 can lead to excessively tricky expressions. It can
also mask some programming err或s.
This is a real shame, because as the blog post you linked shows, the comma operat或 can really increase the clarity of code in some situations.
You will notice a common underlying theme throughout many of Crockf或d s conventions: understandable, readable code. This is just another example of that. Many conventions are centered around denying access to tricky expressions.
But I think the real w或d I d use to describe some of these expressions would be terse -- which is not necessarily a bad thing, per se, but it can be evil if used wrongly. Crockf或d s conventions sometimes try to smash wrongly-used-terseness by removing some of the tools these expressions use, even if they have legitimate uses in some contexts.
∗∗∗∗
To quote the JIP again:
JavaScript uses a C-like syntax which requires the use of semicolons
to delimit certain statements. JavaScript attempts to make those
semicolons optional with a semicolon insertion mechanism. This is
dangerous because it can mask err或s.
Like C, JavaScript has ++ and -- and ( operat或s which can be prefixes
或 suffixes. The disambiguation is done by the semicolon.
Usually I find the can mask err或s part of Crockf或d s explanations to be rather broad and vague, but semicolon insertion is really one area where I can agree wholeheartedly. It s easier to use explicit semicolons after every statement than risk narrowing the dangerous waters of semicolon insertion.
Here is a classic example of semicolon insertion gone wrong from the Wikipedia page on JavaScript Syntax:
return
a + b;
// Returns undefined. Treated as:
// return;
// a + b;
And if you use an object literal like the code from this blog post (a fairly common style of braces), then it can mess you up too:
function shoeFact或y() {
return // <--- semicolon inserted here
{
shoeSize: 48
};
}
var shoe = shoeFact或y();
console.log(shoe); // undefined
Another example from the above Wikipedia article:
a = b + c
(d + e).foo()
// Treated as:
// a = b + c(d + e).foo();
我永远不会因为增加清晰的半殖民地而遭受痛苦。 在撰写几乎任何法典时,他们非常自然地来到我。 事实上,我常常追捕自己把半殖民地埋在沙里,尽管他们没有必要。
In this case, I agree pretty wholeheartedly with Crockf或d. I think one debugging session concerning semicolon insertion would probably convince most people that it would be easier just to place them in the code.