为什么JSLint比Square Bracket更喜欢点符号? [英] Why does JSLint prefer dot notation over Square Bracket?

查看:83
本文介绍了为什么JSLint比Square Bracket更喜欢点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在发现我的一些代码,并且回到了一些错误,说使用点表示法更好。我发现我使用的是方括号表示法(这篇精彩文章清晰明了),但是,我想要问为什么Crockford更喜欢点符号?我正在研究的项目已经使用了SBN,因为它是完整的,我认为它不会让人感到困惑或不可读,但如果用户点有明显的原因,我会纠正它。

I've been linting some of my code and got some errors back saying it is better to use dot notation. I found out I was using square bracket notation (with clarity from this great post), however, I wanted to ask why exactly does Crockford prefer dot notation? The project I'm working on has used SBN for it's entirity, and I don't think it's confusing or un-readable, but if there are distinct reasons to user dot, I will correct it.

只是想在继续之前完全理解它!

Just want to understand it fully before proceeding!

推荐答案

我最了解Crockford,我认为它归结为一致性并避免使用保留字。在他的网站上,他说:

As I best understand Crockford, I think it comes down to consistency and avoiding the use of reserved words. On his site, he states:


当下标是以合法标识符形式的字符串常量
时,可以使用点表示法。由于语言
定义中的错误,保留字不能用于点表示法,但是
它们可以用下标表示法。

The dot notation can be used when the subscript is a string constant in the form of a legal identifier. Because of an error in the language definition, reserved words cannot be used in the dot notation, but they can be used in the subscript notation.

由于您可以参考下标表示法中的保留字,因此可能会引起混淆。基本上,避免使用保留字作为对象成员的名称。点符号强制执行此操作(通过语言 - Crockford称之为错误),因此将其视为更好的编码习惯,以避免使用保留字。

Since you can refer to reserved words in subscript notation, it could cause confusion. Basically, avoid using reserved words as the names of your object's members. The dot notation enforces this (through the language -- an error as Crockford calls it), and so it would be considered a better coding practice to avoid using reserved words.

同样在同一网站上,他还指出点符号更方便。

Also on the same site, he also states that the dot notation is "a little more convenient".

这篇关于为什么JSLint比Square Bracket更喜欢点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆