English 中文(简体)
申辩
原标题:Justifying Crockford claims

I have read Crockford s JavaScript: The Good Parts and have used his validator JSLint. I am sometimes left wondering the justification behind his recommendations. Below is a list of examples I want substantiated.


  1. 如果你没有列入“严格使用”的“密码”;,那么为什么共同提交文件表明存在错误? [见

  2. 为什么在职能范围内使用单一<代码>var进行变式申报? [见 ∗ ∗

  3. 为什么我们需要在<条码>功能和<条码>(<>/条码>(<>条码>)之间留出空间?

  4. 为什么我们可以使用<条码>连续<>。

  5. ++/code>和---有什么错误?

  6. 为什么我们要使用 com操作器,(>>的编码和增减部分除外)? [见,这一博客员额。]

  7. 为什么每个发言都以<代码>结束;? [见,该博客员额。]

最佳回答

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.

  1. 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.
  2. It several restricts eval, which Crockf或d considers to be evil as per the JIP.
  3. 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 vars 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.

问题回答

You re right, Crockford doesn t very often substantiate his practices.

例如,在我看来,界定“<代码>var,但范围之顶以外的其他地方,只要你读到<>aware,则罚款。 贾瓦特人会把他们ist到任何地方。 此外,我认为,它实际上是有益的,因为你能够轻易地从变量的最初价值中抽出,因此,你需要这样做的是看<代码>var<>>。

I also think switch-case fallthroughs are very beneficial if you know how to use them. Particularly for writing something like a very complex state machine.

for s 附有一份声明,但无理的薄膜也属于罚款。 数十年来,人们一直在撰写C。

If you re aware of what constitutes the end of a javascript statement, semicolons aren t necessary either. Semicolon insertion isn t random, it s not undecipherable contrary to popular belief.

众所周知,Crockford正在挖掘可怕的reg。 强力(快速)弹性体可以作为弹性体/规则/图表/手法书写。

For the most part Crockford knows what he s talking about, but don t take his personal style as gospel. It s very unsubstantiated in my opinion. Some of the best javascript coders I ve seen are in direct opposition to Crockford s style with regards to their code.

编辑:我看不出为什么这个问题已经结束。 这个问题可以客观地讨论,值得讨论。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签