Skip to content

Commit b11adc4

Browse files
committed
address anonymous functions
1 parent 907617b commit b11adc4

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

README.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ would be much better to just use ES2015/ES6 classes and simply extend the `Array
579579

580580
**Bad:**
581581
```javascript
582-
Array.prototype.diff = function(comparisonArray) {
582+
Array.prototype.diff = function diff(comparisonArray) {
583583
const values = [];
584584
const hash = {};
585585

@@ -957,7 +957,7 @@ const Employee = function(name) {
957957
this.name = name;
958958
}
959959

960-
Employee.prototype.getName = function() {
960+
Employee.prototype.getName = function getName() {
961961
return this.name;
962962
}
963963

@@ -969,15 +969,11 @@ console.log('Employee name: ' + employee.getName()); // Employee name: undefined
969969

970970
**Good**:
971971
```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+
};
981977

982978
const employee = new Employee('John Doe');
983979
console.log('Employee name: ' + employee.getName()); // Employee name: John Doe
@@ -1262,7 +1258,7 @@ class DOMTraverser {
12621258

12631259
const $ = new DOMTraverser({
12641260
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.
12661262
// ...
12671263
});
12681264

@@ -1296,7 +1292,7 @@ class DOMTraverser {
12961292
const $ = new DOMTraverser({
12971293
rootNode: document.getElementsByTagName('body'),
12981294
options: {
1299-
animationModule: function() {}
1295+
animationModul() {}
13001296
}
13011297
});
13021298
```
@@ -1413,7 +1409,7 @@ const Animal = function(age) {
14131409
this.age = age;
14141410
};
14151411

1416-
Animal.prototype.move = function() {};
1412+
Animal.prototype.move = function move() {};
14171413

14181414
const Mammal = function(age, furColor) {
14191415
if (!(this instanceof Mammal)) {
@@ -1426,7 +1422,7 @@ const Mammal = function(age, furColor) {
14261422

14271423
Mammal.prototype = Object.create(Animal.prototype);
14281424
Mammal.prototype.constructor = Mammal;
1429-
Mammal.prototype.liveBirth = function() {};
1425+
Mammal.prototype.liveBirth = function liveBirth() {};
14301426

14311427
const Human = function(age, furColor, languageSpoken) {
14321428
if (!(this instanceof Human)) {
@@ -1439,7 +1435,7 @@ const Human = function(age, furColor, languageSpoken) {
14391435

14401436
Human.prototype = Object.create(Mammal.prototype);
14411437
Human.prototype.constructor = Human;
1442-
Human.prototype.speak = function() {};
1438+
Human.prototype.speak = function speak() {};
14431439
```
14441440

14451441
**Good:**
@@ -1647,8 +1643,8 @@ or refactoring an existing one.
16471643
```javascript
16481644
const assert = require('assert');
16491645

1650-
describe('MakeMomentJSGreatAgain', function() {
1651-
it('handles date boundaries', function() {
1646+
describe('MakeMomentJSGreatAgain', () => {
1647+
it('handles date boundaries', () => {
16521648
let date;
16531649

16541650
date = new MakeMomentJSGreatAgain('1/1/2015');
@@ -1670,20 +1666,20 @@ describe('MakeMomentJSGreatAgain', function() {
16701666
```javascript
16711667
const assert = require('assert');
16721668

1673-
describe('MakeMomentJSGreatAgain', function() {
1674-
it('handles 30-day months', function() {
1669+
describe('MakeMomentJSGreatAgain', () => {
1670+
it('handles 30-day months', () => {
16751671
const date = new MakeMomentJSGreatAgain('1/1/2015');
16761672
date.addDays(30);
16771673
date.shouldEqual('1/31/2015');
16781674
});
16791675

1680-
it('handles leap year', function() {
1676+
it('handles leap year', () => {
16811677
const date = new MakeMomentJSGreatAgain('2/1/2016');
16821678
date.addDays(28);
16831679
assert.equal('02/29/2016', date);
16841680
});
16851681

1686-
it('handles non-leap year', function() {
1682+
it('handles non-leap year', () => {
16871683
const date = new MakeMomentJSGreatAgain('2/1/2015');
16881684
date.addDays(28);
16891685
assert.equal('03/01/2015', date);
@@ -1699,12 +1695,12 @@ Promises are a built-in global type. Use them!
16991695

17001696
**Bad:**
17011697
```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) => {
17031699
if (err) {
17041700
console.error(err);
17051701
}
17061702
else {
1707-
require('fs').writeFile('article.html', response.body, function(err) {
1703+
require('fs').writeFile('article.html', response.body, (err) => {
17081704
if (err) {
17091705
console.error(err);
17101706
} else {
@@ -1719,13 +1715,13 @@ require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', func
17191715
**Good**:
17201716
```javascript
17211717
require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
1722-
.then(function(response) {
1718+
.then((response) => {
17231719
return require('fs-promise').writeFile('article.html', response);
17241720
})
1725-
.then(function() {
1721+
.then(() => {
17261722
console.log('File written');
17271723
})
1728-
.catch(function(err) {
1724+
.catch((err) => {
17291725
console.error(err);
17301726
})
17311727

@@ -1742,13 +1738,13 @@ today!
17421738
**Bad:**
17431739
```javascript
17441740
require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
1745-
.then(function(response) {
1741+
.then((response) => {
17461742
return require('fs-promise').writeFile('article.html', response);
17471743
})
1748-
.then(function() {
1744+
.then(() => {
17491745
console.log('File written');
17501746
})
1751-
.catch(function(err) {
1747+
.catch((err) => {
17521748
console.error(err);
17531749
})
17541750

0 commit comments

Comments
 (0)