深度JavaScript (Deep JavaScript)
These are what I consider to be some JavaScript best practices that can improve the quality and clarity of your code.
这些是我认为可以提高代码质量和清晰度JavaScript最佳实践。
句法 (Syntax)
使用2个空格进行缩进 (Use 2 spaces for indentation)
In recent years there has been a massive switch from your to two spaces for indentation. For example, two spaces, not longer and no tabs — used by Google, npm, Node.js, Idiomatic, Felix.
近年来,已经从您的空间大幅度切换到两个空间进行缩进。 例如,Google,npm,Node.js,Idiomatic和Felix使用两个空格,不再是空格,也没有制表符。
Keep in mind that don’t use tabs for indentation because tabs are displayed so differently between applications and OS.
请记住,请勿使用制表符进行缩进,因为在应用程序和操作系统之间,制表符的显示方式会有所不同。
除了避免转义,我对字符串使用单引号 (I use single quotes for strings except to avoid escaping)
You’ve seen both 'single quotes'
and "double quotes"
used for writing strings in JavaScript.
您已经看到了用于在JavaScript中编写字符串的'single quotes'
和"double quotes"
。
'abc' === "abc"
But I prefer to use single quotes to avoid escaping.
但是我更喜欢使用单引号来避免转义。
我在关键字之后和左括号之前添加一个空格 (I add a space after keywords and before an opening parenthesis)
In written English, there are no spaces after an opening parenthesis and before a closing parenthesis. I also apply this method to JavaScript.
在书面英语中,左括号后和右括号前没有空格。 我也将此方法应用于JavaScript。
There a space after the keyword function
.
关键字function
后有一个空格。
For anonymous functions, I also add a space as follows.
对于匿名函数,我还添加了一个空格,如下所示。
我在逗号后添加空格 (I add spaces after commas)
Commas have no meaning, but they help us to see the structure and therefore the meaning of the sentence. Put a space after a comma.
逗号没有意义,但是它们可以帮助我们了解句子的结构和含义。 在逗号后放置一个空格。
我将其他语句与花括号放在同一行 (I kept else statements on the same line as their curly braces)
我将条件运算符放在括号中 (I put the conditional operator in parentheses)
This helps with reading. The parentheses are useless for the execution. But useless for the execution doesn’t mean useless for the good reading of your code. You should leave them if it makes more sense to read.
这有助于阅读。 括号对于执行无用。 但是对于执行无用并不意味着对代码的良好阅读无用。 如果阅读起来更有意义,则应将其保留。
变数 (Variables)
我每行声明一个变量 (I declare one variable per line)
Don’t declare multiple variables with a single declaration.
不要用一个声明来声明多个变量。
I declare one variable per line since it is simple to delete, insert and rearrange lines and the lines are automatically indented correctly.
我声明每行一个变量,因为它很容易删除,插入和重新排列行,并且行会自动正确缩进。
不要在两个不同的块中两次声明相同的变量 (Don’t declare the same variable twice in two different blocks)
I don’t do this following.
我下面不这样做。
It should be written the below way.
应该以下面的方式写。
我用/* global */
声明浏览器全局 (I declare browser globals with a /* global */
comment)
Exceptions are: window
, document
, and navigator
.
例外是: window
, document
和navigator
。
/* global
ADSAFE, report, jslint
*/
使用对象的规则 (The rules for using Object)
构造函数名称必须以大写字母开头 (Constructor names must begin with a capital letter)
A constructor name should start with an uppercase letter as follows.
构造函数名称应以大写字母开头,如下所示。
创建实例时,我总是使用构造函数和关键字'new'
(I always use constructors and keyword ‘new'
when creating an instance)
It is important to use constructors because it will protect you against forgetting the new
operators for instantiation in the strict mode
.
使用构造函数很重要,因为它可以保护您避免在strict mode
忘记new
用于实例化的运算符。
It also helps my code better fits into the JavaScript mainstream and is more likely to be portable between frameworks.
它还可以帮助我的代码更好地适合JavaScript主流,并且更有可能在框架之间移植。
In case that a constructor function has no arguments, I still write parentheses.
如果构造函数没有参数,我仍会写上括号。
我给私有属性的名称加上下划线 (I prefix the names of private properties with an underscore)
If you want an object’s private data to be completely safe, you have to use closures but it makes the code becomes more complicated.= and slowers so I prefer to add an underscore for private properties.
如果要使对象的私有数据完全安全,则必须使用闭包,但这会使代码变得更加复杂。=且速度较慢,因此我更愿意为私有属性添加下划线。
其他规定 (Other rules)
我总是使用===
而不是==
(I always use ===
instead of ==
)
The ===
operator will not do the conversion, so if two values are not the same type ===
will simply return false.
===
运算符将不会进行转换,因此,如果两个值的类型不同,则===
只会返回false。
我总是使用类型强制 (I always use type coercion)
Coerce a value to a type via Boolean, Number, String(), Object().
通过布尔,数字,字符串(),对象()将值强制转换为类型。
停止使用关键字this
作为隐式参数 (Stop using the keyword this
as an implicit parameter)
I don’t use the keyword this
as an implicit parameter to keep object-oriented and functional mechanism separate.
我不使用关键字this
作为隐式参数来使面向对象和功能机制保持分离。
我通过in和hasOwnProperty()函数中的关键字检查属性的存在 (I check for the existence of a property via the keyword in and hasOwnProperty() function)
It is safer than comparing a property with undefined
or checking for truthiness.
比将undefined
的属性进行比较或检查真实性要安全得多。
我总是容忍失败 (I always allow a fail fast)
Finally, if you can it’s best to fail fast and to not fail silently
最后,如果可以的话,最好快速失败,不要默默地失败
Useful, right?
有用吧?