Skip to content

Commit af5423b

Browse files
committed
First version
1 parent 69a434e commit af5423b

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

es5/README.md

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1717
1. [](#blocks)
1818
1. [注释](#comments)
1919
1. [空白](#whitespace)
20-
1. [Commas](#commas)
21-
1. [Semicolons](#semicolons)
22-
1. [Type Casting & Coercion](#type-casting--coercion)
23-
1. [Naming Conventions](#naming-conventions)
24-
1. [Accessors](#accessors)
20+
1. [逗号](#commas)
21+
1. [分号](#semicolons)
22+
1. [文件类型](#type-casting--coercion)
23+
1. [命名规则](#naming-conventions)
24+
1. [存取器](#accessors)
2525
1. [Constructors](#constructors)
2626
1. [Events](#events)
2727
1. [Modules](#modules)
@@ -722,7 +722,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
722722
}
723723
```
724724

725-
- 给注释增加 `FIXME``TODO` 的前缀可以帮助其他开发者快速了解这是一个需要复查的问题,或是给需要处理的问题提供一个解决方式。这将有别于常见的注释,因为它们是可操作的。使用 `FIXME -- need to figure this out` 或者 `TODO -- need to implement`
725+
- 给注释增加 `FIXME``TODO` 的前缀可以帮助其他开发者快速了解这是一个需要复查的问题,或是给需要实现的功能提供一个解决方式。这将有别于常见的注释,因为它们是可操作的。使用 `FIXME -- need to figure this out` 或者 `TODO -- need to implement`
726726

727727
- 使用 `// FIXME:` 标注问题。
728728

@@ -935,9 +935,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
935935

936936
**[⬆ 回到顶部](#table-of-contents)**
937937

938-
## Commas
938+
## 逗号
939939

940-
- Leading commas: **Nope.**
940+
- 行首逗号: **不需要**
941941

942942
```javascript
943943
// bad
@@ -971,7 +971,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
971971
};
972972
```
973973

974-
- Additional trailing comma: **Nope.** This can cause problems with IE6/7 and IE9 if it's in quirksmode. Also, in some implementations of ES3 would add length to an array if it had an additional trailing comma. This was clarified in ES5 ([source](http://es5.github.io/#D)):
974+
- 额外的行末逗号:**不需要**。这样做会在 IE6/7 IE9 怪异模式下引起问题。同样,多余的逗号在某些 ES3 的实现里会增加数组的长度。在 ES5 中已经澄清了 ([source](http://es5.github.io/#D))
975975

976976
> Edition 5 clarifies the fact that a trailing comma at the end of an ArrayInitialiser does not add to the length of the array. This is not a semantic change from Edition 3 but some implementations may have previously misinterpreted this.
977977

@@ -1002,9 +1002,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
10021002
**[⬆ 回到顶部](#table-of-contents)**
10031003

10041004

1005-
## Semicolons
1005+
## 分号
10061006

1007-
- **Yup.**
1007+
- **使用分号。**
10081008

10091009
```javascript
10101010
// bad
@@ -1019,22 +1019,22 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
10191019
return name;
10201020
})();
10211021
1022-
// good (guards against the function becoming an argument when two files with IIFEs are concatenated)
1022+
// good (防止函数在两个 IIFE 合并时被当成一个参数
10231023
;(function() {
10241024
var name = 'Skywalker';
10251025
return name;
10261026
})();
10271027
```
10281028

1029-
[Read more](http://stackoverflow.com/a/7365214/1712802).
1029+
[了解更多](http://stackoverflow.com/a/7365214/1712802).
10301030

10311031
**[⬆ 回到顶部](#table-of-contents)**
10321032

10331033

1034-
## Type Casting & Coercion
1034+
## 类型转换
10351035

1036-
- Perform type coercion at the beginning of the statement.
1037-
- Strings:
1036+
- 在语句开始时执行类型转换。
1037+
- 字符串:
10381038

10391039
```javascript
10401040
// => this.reviewScore = 9;
@@ -1052,7 +1052,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
10521052
var totalScore = this.reviewScore + ' total score';
10531053
```
10541054

1055-
- Use `parseInt` for Numbers and always with a radix for type casting.
1055+
- 使用 `parseInt` 转换数字并且总是带上类型转换的基数。
10561056

10571057
```javascript
10581058
var inputValue = '4';
@@ -1076,7 +1076,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
10761076
var val = parseInt(inputValue, 10);
10771077
```
10781078

1079-
- If for whatever reason you are doing something wild and `parseInt` is your bottleneck and need to use Bitshift for [performance reasons](http://jsperf.com/coercion-vs-casting/3), leave a comment explaining why and what you're doing.
1079+
- 如果因为某些原因 `parseInt` 成为你所做的事的瓶颈而需要使用位操作解决[性能问题](http://jsperf.com/coercion-vs-casting/3)时,留个注释说清楚原因和你的目的。
10801080

10811081
```javascript
10821082
// good
@@ -1088,15 +1088,15 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
10881088
var val = inputValue >> 0;
10891089
```
10901090

1091-
- **Note:** Be careful when using bitshift operations. Numbers are represented as [64-bit values](http://es5.github.io/#x4.3.19), but Bitshift operations always return a 32-bit integer ([source](http://es5.github.io/#x11.7)). Bitshift can lead to unexpected behavior for integer values larger than 32 bits. [Discussion](https://github.com/airbnb/javascript/issues/109). Largest signed 32-bit Int is 2,147,483,647:
1091+
- **注:** 小心使用位操作运算符。数字会被当成 [64 位值](http://es5.github.io/#x4.3.19),但是位操作运算符总是返回 32 位的整数([source](http://es5.github.io/#x11.7))。位操作处理大于 32 位的整数值时还会导致意料之外的行为。[讨论](https://github.com/airbnb/javascript/issues/109)。最大的 32 位整数是 2,147,483,647
10921092

10931093
```javascript
10941094
2147483647 >> 0 //=> 2147483647
10951095
2147483648 >> 0 //=> -2147483648
10961096
2147483649 >> 0 //=> -2147483647
10971097
```
10981098

1099-
- Booleans:
1099+
- 布尔:
11001100

11011101
```javascript
11021102
var age = 0;
@@ -1114,9 +1114,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
11141114
**[⬆ 回到顶部](#table-of-contents)**
11151115

11161116

1117-
## Naming Conventions
1117+
## 命名规则
11181118

1119-
- Avoid single letter names. Be descriptive with your naming.
1119+
- 避免单字幕命名。命名应具备描述性。
11201120

11211121
```javascript
11221122
// bad
@@ -1130,7 +1130,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
11301130
}
11311131
```
11321132

1133-
- Use camelCase when naming objects, functions, and instances.
1133+
- 使用驼峰式命名对象、函数和实例。
11341134

11351135
```javascript
11361136
// bad
@@ -1144,7 +1144,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
11441144
function thisIsMyFunction() {}
11451145
```
11461146

1147-
- Use PascalCase when naming constructors or classes.
1147+
- 使用帕斯卡式(所有单词首字母大写)命名构造函数或类。
11481148

11491149
```javascript
11501150
// bad
@@ -1166,7 +1166,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
11661166
});
11671167
```
11681168

1169-
- Use a leading underscore `_` when naming private properties.
1169+
- 使用下划线 `_` 开头命名私有属性。
11701170

11711171
```javascript
11721172
// bad
@@ -1177,7 +1177,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
11771177
this._firstName = 'Panda';
11781178
```
11791179

1180-
- When saving a reference to `this` use `_this`.
1180+
- 使用 `_this` 保存 `this` 的引用。
11811181

11821182
```javascript
11831183
// bad
@@ -1205,7 +1205,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
12051205
}
12061206
```
12071207

1208-
- Name your functions. This is helpful for stack traces.
1208+
- 给函数命名。这在做堆栈轨迹时很有帮助。
12091209

12101210
```javascript
12111211
// bad
@@ -1219,9 +1219,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
12191219
};
12201220
```
12211221

1222-
- **Note:** IE8 and below exhibit some quirks with named function expressions. See [http://kangax.github.io/nfe/](http://kangax.github.io/nfe/) for more info.
1222+
- **注:** IE8 及以下版本对命名函数表达式的处理有些怪异。了解更多信息到 [http://kangax.github.io/nfe/](http://kangax.github.io/nfe/)
12231223

1224-
- If your file exports a single class, your filename should be exactly the name of the class.
1224+
- 如果你的文件导出一个类,你的文件名应该与类名完全相同。
12251225
```javascript
12261226
// file contents
12271227
class CheckBox {
@@ -1243,10 +1243,10 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
12431243
**[⬆ 回到顶部](#table-of-contents)**
12441244

12451245

1246-
## Accessors
1246+
## 存取器
12471247

1248-
- Accessor functions for properties are not required.
1249-
- If you do make accessor functions use getVal() and setVal('hello').
1248+
- 属性的存取函数不是必须的。
1249+
- 如果你需要存取函数时使用 `getVal()``setVal('hello')`
12501250

12511251
```javascript
12521252
// bad
@@ -1262,7 +1262,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
12621262
dragon.setAge(25);
12631263
```
12641264

1265-
- If the property is a boolean, use isVal() or hasVal().
1265+
- 如果属性是布尔值,使用 `isVal()``hasVal()`
12661266

12671267
```javascript
12681268
// bad
@@ -1276,7 +1276,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
12761276
}
12771277
```
12781278

1279-
- It's okay to create get() and set() functions, but be consistent.
1279+
- 创建 get() set() 函数是可以的,但要保持一致。
12801280

12811281
```javascript
12821282
function Jedi(options) {
@@ -1297,9 +1297,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
12971297
**[⬆ 回到顶部](#table-of-contents)**
12981298

12991299

1300-
## Constructors
1300+
## 构造函数
13011301

1302-
- Assign methods to the prototype object, instead of overwriting the prototype with a new object. Overwriting the prototype makes inheritance impossible: by resetting the prototype you'll overwrite the base!
1302+
- 给对象原型分配方法,而不是使用一个新对象覆盖原型。覆盖原型将导致继承出现问题:重设原型将覆盖原有原型!
13031303

13041304
```javascript
13051305
function Jedi() {
@@ -1327,7 +1327,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
13271327
};
13281328
```
13291329

1330-
- Methods can return `this` to help with method chaining.
1330+
- 方法可以返回 `this` 来实现方法链式使用。
13311331

13321332
```javascript
13331333
// bad
@@ -1362,7 +1362,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
13621362
```
13631363

13641364

1365-
- It's okay to write a custom toString() method, just make sure it works successfully and causes no side effects.
1365+
- 写一个自定义的 `toString()` 方法是可以的,但是确保它可以正常工作且不会产生副作用。
13661366

13671367
```javascript
13681368
function Jedi(options) {
@@ -1382,9 +1382,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
13821382
**[⬆ 回到顶部](#table-of-contents)**
13831383

13841384

1385-
## Events
1385+
## 事件
13861386

1387-
- When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass a hash instead of a raw value. This allows a subsequent contributor to add more data to the event payload without finding and updating every handler for the event. For example, instead of:
1387+
- 当给时间附加数据时(无论是 DOM 事件还是私有事件),传入一个哈希而不是原始值。这样可以让后面的贡献者增加更多数据到事件数据而无需找出并更新事件的每一个处理器。例如,不好的写法:
13881388

13891389
```js
13901390
// bad
@@ -1397,7 +1397,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
13971397
});
13981398
```
13991399

1400-
prefer:
1400+
更好的写法:
14011401

14021402
```js
14031403
// good
@@ -1413,12 +1413,12 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
14131413
**[⬆ 回到顶部](#table-of-contents)**
14141414

14151415

1416-
## Modules
1416+
## 模块
14171417

1418-
- The module should start with a `!`. This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. [Explanation](https://github.com/airbnb/javascript/issues/44#issuecomment-13063933)
1419-
- The file should be named with camelCase, live in a folder with the same name, and match the name of the single export.
1420-
- Add a method called `noConflict()` that sets the exported module to the previous version and returns this one.
1421-
- Always declare `'use strict';` at the top of the module.
1418+
- 模块应该以 `!` 开始。这样确保了当一个不好的模块忘记包含最后的分号时,在合并代码到生产环境后不会产生错误。[详细说明](https://github.com/airbnb/javascript/issues/44#issuecomment-13063933)
1419+
- 文件应该以驼峰式命名,并放在同名的文件夹里,且与导出的名字一致。
1420+
- 增加一个名为 `noConflict()` 的方法来设置导出的模块为前一个版本并返回它。
1421+
- 永远在模块顶部声明 `'use strict';`
14221422

14231423
```javascript
14241424
// fancyInput/fancyInput.js
@@ -1446,7 +1446,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
14461446

14471447
## jQuery
14481448

1449-
- Prefix jQuery object variables with a `$`.
1449+
- 使用 `$` 作为存储 jQuery 对象的变量名前缀。
14501450

14511451
```javascript
14521452
// bad
@@ -1456,7 +1456,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
14561456
var $sidebar = $('.sidebar');
14571457
```
14581458

1459-
- Cache jQuery lookups.
1459+
- 缓存 jQuery 查询。
14601460

14611461
```javascript
14621462
// bad
@@ -1483,8 +1483,8 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
14831483
}
14841484
```
14851485

1486-
- For DOM queries use Cascading `$('.sidebar ul')` or parent > child `$('.sidebar > ul')`. [jsPerf](http://jsperf.com/jquery-find-vs-context-sel/16)
1487-
- Use `find` with scoped jQuery object queries.
1486+
- DOM 查询使用层叠 `$('.sidebar ul')` 或 父元素 > 子元素 `$('.sidebar > ul')` [jsPerf](http://jsperf.com/jquery-find-vs-context-sel/16)
1487+
- 对有作用域的 jQuery 对象查询使用 `find`
14881488

14891489
```javascript
14901490
// bad
@@ -1506,14 +1506,14 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
15061506
**[⬆ 回到顶部](#table-of-contents)**
15071507

15081508

1509-
## ECMAScript 5 Compatibility
1509+
## ECMAScript 5 兼容性
15101510

1511-
- Refer to [Kangax](https://twitter.com/kangax/)'s ES5 [compatibility table](http://kangax.github.com/es5-compat-table/).
1511+
- 参考 [Kangax](https://twitter.com/kangax/)ES5 [兼容表](http://kangax.github.com/es5-compat-table/).
15121512

15131513
**[⬆ 回到顶部](#table-of-contents)**
15141514

15151515

1516-
## Testing
1516+
## 测试
15171517

15181518
- **Yup.**
15191519

@@ -1526,7 +1526,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
15261526
**[⬆ 回到顶部](#table-of-contents)**
15271527

15281528

1529-
## Performance
1529+
## 性能
15301530

15311531
- [On Layout & Web Performance](http://kellegous.com/j/2013/01/26/layout-performance/)
15321532
- [String vs Array Concat](http://jsperf.com/string-vs-array-concat/2)
@@ -1540,7 +1540,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
15401540
**[⬆ 回到顶部](#table-of-contents)**
15411541

15421542

1543-
## Resources
1543+
## 相关资源
15441544

15451545

15461546
**Read This**

0 commit comments

Comments
 (0)