File tree
Expand file treeCollapse file tree1 file changed
+10
-14
lines changed Expand file treeCollapse file tree1 file changed
+10
-14
lines changed Original file line number | Diff line number | Diff line change |
---|
@@ -1114,10 +1114,10 @@ and you can chain further class methods onto it.
|
1114 | 1114 | **Bad:**
|
1115 | 1115 | ```javascript
|
1116 | 1116 | class Car {
|
1117 |
| -constructor() { |
1118 |
| -this.make = 'Honda'; |
1119 |
| -this.model = 'Accord'; |
1120 |
| -this.color = 'white'; |
| 1117 | +constructor(make, model, color) { |
| 1118 | +this.make = make; |
| 1119 | +this.model = model; |
| 1120 | +this.color = color; |
1121 | 1121 | }
|
1122 | 1122 |
|
1123 | 1123 | setMake(make) {
|
@@ -1137,20 +1137,18 @@ class Car {
|
1137 | 1137 | }
|
1138 | 1138 | }
|
1139 | 1139 |
|
1140 |
| -const car = new Car(); |
| 1140 | +const car = new Car('Ford','F-150','red'); |
1141 | 1141 | car.setColor('pink');
|
1142 |
| -car.setMake('Ford'); |
1143 |
| -car.setModel('F-150'); |
1144 | 1142 | car.save();
|
1145 | 1143 | ```
|
1146 | 1144 |
|
1147 | 1145 | **Good:**
|
1148 | 1146 | ```javascript
|
1149 | 1147 | class Car {
|
1150 |
| -constructor() { |
1151 |
| -this.make = ''; |
1152 |
| -this.model = ''; |
1153 |
| -this.color = ''; |
| 1148 | +constructor(make, model, color) { |
| 1149 | +this.make = make; |
| 1150 | +this.model = model; |
| 1151 | +this.color = color; |
1154 | 1152 | }
|
1155 | 1153 |
|
1156 | 1154 | setMake(make) {
|
@@ -1178,10 +1176,8 @@ class Car {
|
1178 | 1176 | }
|
1179 | 1177 | }
|
1180 | 1178 |
|
1181 |
| -const car = new Car() |
| 1179 | +const car = new Car('Ford','F-150','red') |
1182 | 1180 | .setColor('pink')
|
1183 |
| -.setMake('Ford') |
1184 |
| -.setModel('F-150') |
1185 | 1181 | .save();
|
1186 | 1182 | ```
|
1187 | 1183 | **[⬆ back to top](#table-of-contents)**
|
|
You can’t perform that action at this time.
0 commit comments