@@ -17,11 +17,11 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
17
17
1 . [ 块] ( #blocks )
18
18
1 . [ 注释] ( #comments )
19
19
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 )
25
25
1 . [ Constructors] ( #constructors )
26
26
1 . [ Events] ( #events )
27
27
1 . [ Modules] ( #modules )
@@ -722,7 +722,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
722
722
}
723
723
` ` `
724
724
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` 。
726
726
727
727
- 使用 ` // FIXME:` 标注问题。
728
728
@@ -935,9 +935,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
935
935
936
936
** [⬆ 回到顶部](#table- of - contents)**
937
937
938
- ## Commas
938
+ ## 逗号
939
939
940
- - Leading commas : ** Nope. **
940
+ - 行首逗号 : ** 不需要 ** 。
941
941
942
942
` ` ` javascript
943
943
// bad
@@ -971,7 +971,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
971
971
};
972
972
` ` `
973
973
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)):
975
975
976
976
> 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 .
977
977
@@ -1002,9 +1002,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1002
1002
** [⬆ 回到顶部](#table- of - contents)**
1003
1003
1004
1004
1005
- ## Semicolons
1005
+ ## 分号
1006
1006
1007
- - **Yup. **
1007
+ - ** 使用分号。 **
1008
1008
1009
1009
` ` ` javascript
1010
1010
// bad
@@ -1019,22 +1019,22 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1019
1019
return name;
1020
1020
})();
1021
1021
1022
- // good (guards against the function becoming an argument when two files with IIFEs are concatenated)
1022
+ // good (防止函数在两个 IIFE 合并时被当成一个参数
1023
1023
;(function() {
1024
1024
var name = 'Skywalker';
1025
1025
return name;
1026
1026
})();
1027
1027
` ` `
1028
1028
1029
- [Read more ](http://stackoverflow.com/a/7365214/1712802).
1029
+ [了解更多 ](http: // stackoverflow.com/a/7365214/1712802).
1030
1030
1031
1031
** [⬆ 回到顶部](#table- of - contents)**
1032
1032
1033
1033
1034
- ## Type Casting & Coercion
1034
+ ## 类型转换
1035
1035
1036
- - Perform type coercion at the beginning of the statement.
1037
- - Strings:
1036
+ - 在语句开始时执行类型转换。
1037
+ - 字符串:
1038
1038
1039
1039
` ` ` javascript
1040
1040
// => this.reviewScore = 9;
@@ -1052,7 +1052,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1052
1052
var totalScore = this.reviewScore + ' total score';
1053
1053
` ` `
1054
1054
1055
- - Use `parseInt` for Numbers and always with a radix for type casting.
1055
+ - 使用 ` parseInt` 转换数字并且总是带上类型转换的基数。
1056
1056
1057
1057
` ` ` javascript
1058
1058
var inputValue = '4';
@@ -1076,7 +1076,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1076
1076
var val = parseInt(inputValue, 10);
1077
1077
` ` `
1078
1078
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)时,留个注释说清楚原因和你的目的。
1080
1080
1081
1081
` ` ` javascript
1082
1082
// good
@@ -1088,15 +1088,15 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1088
1088
var val = inputValue >> 0;
1089
1089
` ` `
1090
1090
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:
1092
1092
1093
1093
` ` ` javascript
1094
1094
2147483647 >> 0 //=> 2147483647
1095
1095
2147483648 >> 0 //=> -2147483648
1096
1096
2147483649 >> 0 //=> -2147483647
1097
1097
` ` `
1098
1098
1099
- - Booleans :
1099
+ - 布尔 :
1100
1100
1101
1101
` ` ` javascript
1102
1102
var age = 0;
@@ -1114,9 +1114,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1114
1114
** [⬆ 回到顶部](#table- of - contents)**
1115
1115
1116
1116
1117
- ## Naming Conventions
1117
+ ## 命名规则
1118
1118
1119
- - Avoid single letter names . Be descriptive with your naming.
1119
+ - 避免单字幕命名。命名应具备描述性。
1120
1120
1121
1121
` ` ` javascript
1122
1122
// bad
@@ -1130,7 +1130,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1130
1130
}
1131
1131
` ` `
1132
1132
1133
- - Use camelCase when naming objects, functions, and instances.
1133
+ - 使用驼峰式命名对象、函数和实例。
1134
1134
1135
1135
` ` ` javascript
1136
1136
// bad
@@ -1144,7 +1144,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1144
1144
function thisIsMyFunction() {}
1145
1145
` ` `
1146
1146
1147
- - Use PascalCase when naming constructors or classes.
1147
+ - 使用帕斯卡式(所有单词首字母大写)命名构造函数或类。
1148
1148
1149
1149
` ` ` javascript
1150
1150
// bad
@@ -1166,7 +1166,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1166
1166
});
1167
1167
` ` `
1168
1168
1169
- - Use a leading underscore ` _` when naming private properties.
1169
+ - 使用下划线 ` _` 开头命名私有属性。
1170
1170
1171
1171
` ` ` javascript
1172
1172
// bad
@@ -1177,7 +1177,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1177
1177
this._firstName = 'Panda';
1178
1178
` ` `
1179
1179
1180
- - When saving a reference to ` this ` use ` _this ` .
1180
+ - 使用 ` _this ` 保存 ` this ` 的引用。
1181
1181
1182
1182
` ` ` javascript
1183
1183
// bad
@@ -1205,7 +1205,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1205
1205
}
1206
1206
` ` `
1207
1207
1208
- - Name your functions . This is helpful for stack traces.
1208
+ - 给函数命名。这在做堆栈轨迹时很有帮助。
1209
1209
1210
1210
` ` ` javascript
1211
1211
// bad
@@ -1219,9 +1219,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1219
1219
};
1220
1220
` ` `
1221
1221
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/)。
1223
1223
1224
- - If your file exports a single class, your filename should be exactly the name of the class.
1224
+ - 如果你的文件导出一个类,你的文件名应该与类名完全相同。
1225
1225
` ` ` javascript
1226
1226
// file contents
1227
1227
class CheckBox {
@@ -1243,10 +1243,10 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1243
1243
** [⬆ 回到顶部](#table- of - contents)**
1244
1244
1245
1245
1246
- ## Accessors
1246
+ ## 存取器
1247
1247
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')` 。
1250
1250
1251
1251
` ` ` javascript
1252
1252
// bad
@@ -1262,7 +1262,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1262
1262
dragon.setAge(25);
1263
1263
` ` `
1264
1264
1265
- - If the property is a boolean, use isVal() or hasVal().
1265
+ - 如果属性是布尔值,使用 ` isVal()` 或 ` hasVal()` 。
1266
1266
1267
1267
` ` ` javascript
1268
1268
// bad
@@ -1276,7 +1276,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1276
1276
}
1277
1277
` ` `
1278
1278
1279
- - It's okay to create get() and set() functions, but be consistent.
1279
+ - 创建 get () 和 set () 函数是可以的,但要保持一致。
1280
1280
1281
1281
` ` ` javascript
1282
1282
function Jedi(options) {
@@ -1297,9 +1297,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1297
1297
** [⬆ 回到顶部](#table- of - contents)**
1298
1298
1299
1299
1300
- ## Constructors
1300
+ ## 构造函数
1301
1301
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
+ - 给对象原型分配方法,而不是使用一个新对象覆盖原型。覆盖原型将导致继承出现问题:重设原型将覆盖原有原型!
1303
1303
1304
1304
` ` ` javascript
1305
1305
function Jedi() {
@@ -1327,7 +1327,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1327
1327
};
1328
1328
` ` `
1329
1329
1330
- - Methods can return ` this ` to help with method chaining.
1330
+ - 方法可以返回 ` this` 来实现方法链式使用。
1331
1331
1332
1332
` ` ` javascript
1333
1333
// bad
@@ -1362,7 +1362,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1362
1362
` ` `
1363
1363
1364
1364
1365
- - It's okay to write a custom toString() method, just make sure it works successfully and causes no side effects.
1365
+ - 写一个自定义的 ` toString()` 方法是可以的,但是确保它可以正常工作且不会产生副作用。
1366
1366
1367
1367
` ` ` javascript
1368
1368
function Jedi(options) {
@@ -1382,9 +1382,9 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1382
1382
** [⬆ 回到顶部](#table- of - contents)**
1383
1383
1384
1384
1385
- ## Events
1385
+ ## 事件
1386
1386
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 事件还是私有事件),传入一个哈希而不是原始值。这样可以让后面的贡献者增加更多数据到事件数据而无需找出并更新事件的每一个处理器。例如,不好的写法:
1388
1388
1389
1389
` ` ` js
1390
1390
// bad
@@ -1397,7 +1397,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1397
1397
});
1398
1398
` ` `
1399
1399
1400
- prefer:
1400
+ 更好的写法:
1401
1401
1402
1402
` ` ` js
1403
1403
// good
@@ -1413,12 +1413,12 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1413
1413
** [⬆ 回到顶部](#table- of - contents)**
1414
1414
1415
1415
1416
- ## Modules
1416
+ ## 模块
1417
1417
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';` 。
1422
1422
1423
1423
` ` ` javascript
1424
1424
// fancyInput/fancyInput.js
@@ -1446,7 +1446,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1446
1446
1447
1447
## jQuery
1448
1448
1449
- - Prefix jQuery object variables with a ` $` .
1449
+ - 使用 ` $` 作为存储 jQuery 对象的变量名前缀。
1450
1450
1451
1451
` ` ` javascript
1452
1452
// bad
@@ -1456,7 +1456,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1456
1456
var $sidebar = $('.sidebar');
1457
1457
` ` `
1458
1458
1459
- - Cache jQuery lookups.
1459
+ - 缓存 jQuery 查询。
1460
1460
1461
1461
` ` ` javascript
1462
1462
// bad
@@ -1483,8 +1483,8 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1483
1483
}
1484
1484
` ` `
1485
1485
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 ` 。
1488
1488
1489
1489
` ` ` javascript
1490
1490
// bad
@@ -1506,14 +1506,14 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1506
1506
** [⬆ 回到顶部](#table- of - contents)**
1507
1507
1508
1508
1509
- ## ECMAScript 5 Compatibility
1509
+ ## ECMAScript 5 兼容性
1510
1510
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/).
1512
1512
1513
1513
** [⬆ 回到顶部](#table- of - contents)**
1514
1514
1515
1515
1516
- ## Testing
1516
+ ## 测试
1517
1517
1518
1518
- ** Yup.**
1519
1519
@@ -1526,7 +1526,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1526
1526
** [⬆ 回到顶部](#table- of - contents)**
1527
1527
1528
1528
1529
- ## Performance
1529
+ ## 性能
1530
1530
1531
1531
- [On Layout & Web Performance ](http: // kellegous.com/j/2013/01/26/layout-performance/)
1532
1532
- [String vs Array Concat](http: // jsperf.com/string-vs-array-concat/2)
@@ -1540,7 +1540,7 @@ Airbnb JavaScript 代码规范,[英文版](https://github.com/airbnb/javascrip
1540
1540
** [⬆ 回到顶部](#table- of - contents)**
1541
1541
1542
1542
1543
- ## Resources
1543
+ ## 相关资源
1544
1544
1545
1545
1546
1546
** Read This**
0 commit comments