@@ -579,7 +579,7 @@ would be much better to just use ES2015/ES6 classes and simply extend the `Array
579
579
580
580
** Bad:**
581
581
``` javascript
582
- Array .prototype .diff = function (comparisonArray ) {
582
+ Array .prototype .diff = function diff (comparisonArray ) {
583
583
const values = [];
584
584
const hash = {};
585
585
@@ -957,7 +957,7 @@ const Employee = function(name) {
957
957
this .name = name;
958
958
}
959
959
960
- Employee .prototype .getName = function () {
960
+ Employee .prototype .getName = function getName () {
961
961
return this .name ;
962
962
}
963
963
@@ -969,15 +969,11 @@ console.log('Employee name: ' + employee.getName()); // Employee name: undefined
969
969
970
970
** Good** :
971
971
``` javascript
972
- const Employee = (function () {
973
- function Employee (name ) {
974
- this .getName = function () {
975
- return name;
976
- };
977
- }
978
-
979
- return Employee;
980
- }());
972
+ const Employee = function (name ) {
973
+ this .getName = function getName () {
974
+ return name;
975
+ };
976
+ };
981
977
982
978
const employee = new Employee (' John Doe' );
983
979
console .log (' Employee name: ' + employee .getName ()); // Employee name: John Doe
@@ -1262,7 +1258,7 @@ class DOMTraverser {
1262
1258
1263
1259
const $ = new DOMTraverser ({
1264
1260
rootNode: document .getElementsByTagName (' body' ),
1265
- animationModule : function () {} // Most of the time, we won't need to animate when traversing.
1261
+ animationModule () {} // Most of the time, we won't need to animate when traversing.
1266
1262
// ...
1267
1263
});
1268
1264
@@ -1296,7 +1292,7 @@ class DOMTraverser {
1296
1292
const $ = new DOMTraverser ({
1297
1293
rootNode: document .getElementsByTagName (' body' ),
1298
1294
options: {
1299
- animationModule : function () {}
1295
+ animationModul () {}
1300
1296
}
1301
1297
});
1302
1298
```
@@ -1413,7 +1409,7 @@ const Animal = function(age) {
1413
1409
this .age = age;
1414
1410
};
1415
1411
1416
- Animal .prototype .move = function () {};
1412
+ Animal .prototype .move = function move () {};
1417
1413
1418
1414
const Mammal = function (age , furColor ) {
1419
1415
if (! (this instanceof Mammal)) {
@@ -1426,7 +1422,7 @@ const Mammal = function(age, furColor) {
1426
1422
1427
1423
Mammal .prototype = Object .create (Animal .prototype );
1428
1424
Mammal .prototype .constructor = Mammal;
1429
- Mammal .prototype .liveBirth = function () {};
1425
+ Mammal .prototype .liveBirth = function liveBirth () {};
1430
1426
1431
1427
const Human = function (age , furColor , languageSpoken ) {
1432
1428
if (! (this instanceof Human)) {
@@ -1439,7 +1435,7 @@ const Human = function(age, furColor, languageSpoken) {
1439
1435
1440
1436
Human .prototype = Object .create (Mammal .prototype );
1441
1437
Human .prototype .constructor = Human;
1442
- Human .prototype .speak = function () {};
1438
+ Human .prototype .speak = function speak () {};
1443
1439
```
1444
1440
1445
1441
** Good:**
@@ -1647,8 +1643,8 @@ or refactoring an existing one.
1647
1643
``` javascript
1648
1644
const assert = require (' assert' );
1649
1645
1650
- describe (' MakeMomentJSGreatAgain' , function () {
1651
- it (' handles date boundaries' , function () {
1646
+ describe (' MakeMomentJSGreatAgain' , () => {
1647
+ it (' handles date boundaries' , () => {
1652
1648
let date;
1653
1649
1654
1650
date = new MakeMomentJSGreatAgain (' 1/1/2015' );
@@ -1670,20 +1666,20 @@ describe('MakeMomentJSGreatAgain', function() {
1670
1666
``` javascript
1671
1667
const assert = require (' assert' );
1672
1668
1673
- describe (' MakeMomentJSGreatAgain' , function () {
1674
- it (' handles 30-day months' , function () {
1669
+ describe (' MakeMomentJSGreatAgain' , () => {
1670
+ it (' handles 30-day months' , () => {
1675
1671
const date = new MakeMomentJSGreatAgain (' 1/1/2015' );
1676
1672
date .addDays (30 );
1677
1673
date .shouldEqual (' 1/31/2015' );
1678
1674
});
1679
1675
1680
- it (' handles leap year' , function () {
1676
+ it (' handles leap year' , () => {
1681
1677
const date = new MakeMomentJSGreatAgain (' 2/1/2016' );
1682
1678
date .addDays (28 );
1683
1679
assert .equal (' 02/29/2016' , date);
1684
1680
});
1685
1681
1686
- it (' handles non-leap year' , function () {
1682
+ it (' handles non-leap year' , () => {
1687
1683
const date = new MakeMomentJSGreatAgain (' 2/1/2015' );
1688
1684
date .addDays (28 );
1689
1685
assert .equal (' 03/01/2015' , date);
@@ -1699,12 +1695,12 @@ Promises are a built-in global type. Use them!
1699
1695
1700
1696
** Bad:**
1701
1697
``` javascript
1702
- require (' request' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' , function (err , response ) {
1698
+ require (' request' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' , (err , response ) => {
1703
1699
if (err) {
1704
1700
console .error (err);
1705
1701
}
1706
1702
else {
1707
- require (' fs' ).writeFile (' article.html' , response .body , function (err ) {
1703
+ require (' fs' ).writeFile (' article.html' , response .body , (err ) => {
1708
1704
if (err) {
1709
1705
console .error (err);
1710
1706
} else {
@@ -1719,13 +1715,13 @@ require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', func
1719
1715
** Good** :
1720
1716
``` javascript
1721
1717
require (' request-promise' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' )
1722
- .then (function (response ) {
1718
+ .then ((response ) => {
1723
1719
return require (' fs-promise' ).writeFile (' article.html' , response);
1724
1720
})
1725
- .then (function () {
1721
+ .then (() => {
1726
1722
console .log (' File written' );
1727
1723
})
1728
- .catch (function (err ) {
1724
+ .catch ((err ) => {
1729
1725
console .error (err);
1730
1726
})
1731
1727
@@ -1742,13 +1738,13 @@ today!
1742
1738
** Bad:**
1743
1739
``` javascript
1744
1740
require (' request-promise' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' )
1745
- .then (function (response ) {
1741
+ .then ((response ) => {
1746
1742
return require (' fs-promise' ).writeFile (' article.html' , response);
1747
1743
})
1748
- .then (function () {
1744
+ .then (() => {
1749
1745
console .log (' File written' );
1750
1746
})
1751
- .catch (function (err ) {
1747
+ .catch ((err ) => {
1752
1748
console .error (err);
1753
1749
})
1754
1750
0 commit comments