为什么JSLint希望我使用双引号而不是单引号?期待'''而不是看'''

时间:2022-10-10 22:26:50

I recently started seeing JSLint giving the following warning:

我最近开始看到JSLint发出以下警告:

JSLint: Expected '"' and instead saw '''.

JSLint:期待'''而不是看'''。

In other words, it's wanting me to use double quotes instead of single quotes. Why is JSLint warning me about that? Aren't single quotes and double quotes more or less interchangeable in JavaScript, subject only to the programmer's preference?

换句话说,它希望我使用双引号而不是单引号。为什么JSLint会对此发出警告?在JavaScript中,单引号和双引号是否可以互换?仅受程序员的偏好?

2 个解决方案

#1


27  

The 2016-02-07 version of JSLint changed to prefer double quotes over single quotes. Douglas Crockford, developer of JSLint and influential developer for JavaScript and JSON, provided this rationale:

2016-02-07版本的JSLint更改为使用双引号而不是单引号。 JSLint的开发人员和JavaScript和JSON的有影响力的开发人员Douglas Crockford提供了这样的理由:

When I first met JavaScript, it [sic] was surprised that it had the two kinds of quotes, and I tried to make sense of it, using single for internal text, and double for external.

当我第一次遇到JavaScript时,它[sic]很惊讶它有两种引号,我试图理解它,使用单个内部文本,双重外部。

But eventually I realized that distinction isn't worth the clutter and confusion that comes from having two when only one is needed. I decided to go with double because that is what JSON uses, and it avoids errors caused by the overloading of apostrophe. I have been bitten by that.

但最终我意识到区别不值得因为只需要一个人而产生的混乱和混乱。我决定使用double,因为这是JSON使用的,它避免了由撇号重载引起的错误。我被这个咬了。

In general, I am looking for ways to make the language smaller and better. Quotes fall in the same class as null & undefined. We really don't need both.

总的来说,我正在寻找使语言更小更好的方法。引号与null和undefined属于同一类。我们真的不需要两者。

I tried it out on some of my own code, and I think it is an improvement. Eventually, I may add option.single to JSLint.

我尝试了一些自己的代码,我认为这是一个改进。最后,我可以将option.single添加到JSLint。

This did happen: option.single was added in the 2016-06-09 version of JSLint, so you can now tell JSLint to optionally ignore single quotes.

确实发生了这种情况:在2016-06-09版本的JSLint中添加了option.single,因此您现在可以告诉JSLint可选地忽略单引号。

Crockford more succinctly reiterated his rationale for double quotes over single quotes in a later discussion:

在稍后的讨论中,克罗克福德更简洁地重申了他对单引号双引号的理由:

I found that people had some difficulty managing the two types of quotes. Since the second set is completely unnecessary, and since the second set [single quotes] can introduce problems and confusions, I now recommend using double quotes only.

我发现人们在管理这两种报价时遇到了一些困难。由于第二组完全没有必要,并且由于第二组[单引号]可能引入问题和混淆,我现在建议仅使用双引号。

#2


9  

This is a spam warning from a very opinionated tool, there's no good reason for it.

这是一个非常固执的工具的垃圾邮件警告,没有充分的理由。

The old lint tools (JSLint and JSHint) have been wholly superseded by ESLint, which supports more rules and allows you configure the options you're actually interested in. Specifically, the quotes rule allows you to select single, double, or ignore quoting entirely.

旧的lint工具(JSLint和JSHint)已被ESLint完全取代,ESLint支持更多规则并允许您配置您真正感兴趣的选项。具体来说,引号规则允许您选择单引号,双引号或完全忽略引用。

#1


27  

The 2016-02-07 version of JSLint changed to prefer double quotes over single quotes. Douglas Crockford, developer of JSLint and influential developer for JavaScript and JSON, provided this rationale:

2016-02-07版本的JSLint更改为使用双引号而不是单引号。 JSLint的开发人员和JavaScript和JSON的有影响力的开发人员Douglas Crockford提供了这样的理由:

When I first met JavaScript, it [sic] was surprised that it had the two kinds of quotes, and I tried to make sense of it, using single for internal text, and double for external.

当我第一次遇到JavaScript时,它[sic]很惊讶它有两种引号,我试图理解它,使用单个内部文本,双重外部。

But eventually I realized that distinction isn't worth the clutter and confusion that comes from having two when only one is needed. I decided to go with double because that is what JSON uses, and it avoids errors caused by the overloading of apostrophe. I have been bitten by that.

但最终我意识到区别不值得因为只需要一个人而产生的混乱和混乱。我决定使用double,因为这是JSON使用的,它避免了由撇号重载引起的错误。我被这个咬了。

In general, I am looking for ways to make the language smaller and better. Quotes fall in the same class as null & undefined. We really don't need both.

总的来说,我正在寻找使语言更小更好的方法。引号与null和undefined属于同一类。我们真的不需要两者。

I tried it out on some of my own code, and I think it is an improvement. Eventually, I may add option.single to JSLint.

我尝试了一些自己的代码,我认为这是一个改进。最后,我可以将option.single添加到JSLint。

This did happen: option.single was added in the 2016-06-09 version of JSLint, so you can now tell JSLint to optionally ignore single quotes.

确实发生了这种情况:在2016-06-09版本的JSLint中添加了option.single,因此您现在可以告诉JSLint可选地忽略单引号。

Crockford more succinctly reiterated his rationale for double quotes over single quotes in a later discussion:

在稍后的讨论中,克罗克福德更简洁地重申了他对单引号双引号的理由:

I found that people had some difficulty managing the two types of quotes. Since the second set is completely unnecessary, and since the second set [single quotes] can introduce problems and confusions, I now recommend using double quotes only.

我发现人们在管理这两种报价时遇到了一些困难。由于第二组完全没有必要,并且由于第二组[单引号]可能引入问题和混淆,我现在建议仅使用双引号。

#2


9  

This is a spam warning from a very opinionated tool, there's no good reason for it.

这是一个非常固执的工具的垃圾邮件警告,没有充分的理由。

The old lint tools (JSLint and JSHint) have been wholly superseded by ESLint, which supports more rules and allows you configure the options you're actually interested in. Specifically, the quotes rule allows you to select single, double, or ignore quoting entirely.

旧的lint工具(JSLint和JSHint)已被ESLint完全取代,ESLint支持更多规则并允许您配置您真正感兴趣的选项。具体来说,引号规则允许您选择单引号,双引号或完全忽略引用。