Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c613772

Browse files
Merge pull request ryanmcdermott#88 from vsemozhetbyt/class-methods-use-this
correct using of `this` in examples
2 parents d971254 + f3f6261 commit c613772

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

README.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -744,11 +744,11 @@ class Airplane {
744744
getCruisingAltitude() {
745745
switch (this.type) {
746746
case '777':
747-
return getMaxAltitude() - getPassengerCount();
747+
return this.getMaxAltitude() - this.getPassengerCount();
748748
case 'Air Force One':
749-
return getMaxAltitude();
749+
return this.getMaxAltitude();
750750
case 'Cessna':
751-
return getMaxAltitude() - getFuelExpenditure();
751+
return this.getMaxAltitude() - this.getFuelExpenditure();
752752
}
753753
}
754754
}
@@ -763,21 +763,21 @@ class Airplane {
763763
class Boeing777 extends Airplane {
764764
// ...
765765
getCruisingAltitude() {
766-
return getMaxAltitude() - getPassengerCount();
766+
return this.getMaxAltitude() - this.getPassengerCount();
767767
}
768768
}
769769

770770
class AirForceOne extends Airplane {
771771
// ...
772772
getCruisingAltitude() {
773-
return getMaxAltitude();
773+
return this.getMaxAltitude();
774774
}
775775
}
776776

777777
class Cessna extends Airplane {
778778
// ...
779779
getCruisingAltitude() {
780-
return getMaxAltitude() - getFuelExpenditure();
780+
return this.getMaxAltitude() - this.getFuelExpenditure();
781781
}
782782
}
783783
```
@@ -1009,12 +1009,12 @@ class UserSettings {
10091009
}
10101010

10111011
changeSettings(settings) {
1012-
if (this.verifyCredentials(user)) {
1012+
if (this.verifyCredentials()) {
10131013
// ...
10141014
}
10151015
}
10161016

1017-
verifyCredentials(user) {
1017+
verifyCredentials() {
10181018
// ...
10191019
}
10201020
}
@@ -1410,35 +1410,35 @@ classes until you find yourself needing larger and more complex objects.
14101410
**Bad:**
14111411
```javascript
14121412
const Animal = function(age) {
1413-
if (!(this instanceof Animal)) {
1414-
throw new Error("Instantiate Animal with `new`");
1415-
}
1413+
if (!(this instanceof Animal)) {
1414+
throw new Error("Instantiate Animal with `new`");
1415+
}
14161416

1417-
this.age = age;
1417+
this.age = age;
14181418
};
14191419

14201420
Animal.prototype.move = function() {};
14211421

14221422
const Mammal = function(age, furColor) {
1423-
if (!(this instanceof Mammal)) {
1424-
throw new Error("Instantiate Mammal with `new`");
1425-
}
1423+
if (!(this instanceof Mammal)) {
1424+
throw new Error("Instantiate Mammal with `new`");
1425+
}
14261426

1427-
Animal.call(this, age);
1428-
this.furColor = furColor;
1427+
Animal.call(this, age);
1428+
this.furColor = furColor;
14291429
};
14301430

14311431
Mammal.prototype = Object.create(Animal.prototype);
14321432
Mammal.prototype.constructor = Mammal;
14331433
Mammal.prototype.liveBirth = function() {};
14341434

14351435
const Human = function(age, furColor, languageSpoken) {
1436-
if (!(this instanceof Human)) {
1437-
throw new Error("Instantiate Human with `new`");
1438-
}
1436+
if (!(this instanceof Human)) {
1437+
throw new Error("Instantiate Human with `new`");
1438+
}
14391439

1440-
Mammal.call(this, age, furColor);
1441-
this.languageSpoken = languageSpoken;
1440+
Mammal.call(this, age, furColor);
1441+
this.languageSpoken = languageSpoken;
14421442
};
14431443

14441444
Human.prototype = Object.create(Mammal.prototype);
@@ -1449,29 +1449,29 @@ Human.prototype.speak = function() {};
14491449
**Good:**
14501450
```javascript
14511451
class Animal {
1452-
constructor(age) {
1453-
this.age = age;
1454-
}
1452+
constructor(age) {
1453+
this.age = age;
1454+
}
14551455

1456-
move() {}
1456+
move() { /* ... */ }
14571457
}
14581458

14591459
class Mammal extends Animal {
1460-
constructor(age, furColor) {
1461-
super(age);
1462-
this.furColor = furColor;
1463-
}
1460+
constructor(age, furColor) {
1461+
super(age);
1462+
this.furColor = furColor;
1463+
}
14641464

1465-
liveBirth() {}
1465+
liveBirth() { /* ... */ }
14661466
}
14671467

14681468
class Human extends Mammal {
1469-
constructor(age, furColor, languageSpoken) {
1470-
super(age, furColor);
1471-
this.languageSpoken = languageSpoken;
1472-
}
1469+
constructor(age, furColor, languageSpoken) {
1470+
super(age, furColor);
1471+
this.languageSpoken = languageSpoken;
1472+
}
14731473

1474-
speak() {}
1474+
speak() { /* ... */ }
14751475
}
14761476
```
14771477
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)