You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript doesn't have interfaces so this principle doesn't apply as strictly
1286
-
as others. However, it's important and relevant even with JavaScript's lack of
1287
-
type system.
1288
-
1289
-
ISP states that "Clients should not be forced to depend upon interfaces that
1290
-
they do not use." Interfaces are implicit contracts in JavaScript because of
1291
-
duck typing.
1292
-
1293
-
A good example to look at that demonstrates this principle in JavaScript is for
1294
-
classes that require large settings objects. Not requiring clients to setup
1295
-
huge amounts of options is beneficial, because most of the time they won't need
1296
-
all of the settings. Making them optional helps prevent having a "fat interface".
1285
+
ISP states that "___Clients should not be forced to depend upon interfaces that
1286
+
they do not use.___" In JavaScript, an interface is the contract of an object (i.e. the public properties and methods). In the "Bad" example below, the `Feedback` class inherits everything from the `Message` class, but only a couple of the features will
1287
+
make sense to the subclass.
1297
1288
1298
1289
**Bad:**
1299
1290
```javascript
1300
-
classDOMTraverser {
1301
-
constructor(settings) {
1302
-
this.settings=settings;
1303
-
this.setup();
1291
+
classMessage {
1292
+
constructor() {
1293
+
this._feedback=[];
1294
+
this._rating=0;
1304
1295
}
1305
1296
1306
-
setup() {
1307
-
this.rootNode=this.settings.rootNode;
1308
-
this.animationModule.setup();
1297
+
addFeedback(message) {
1298
+
this._feedback.push(message);
1309
1299
}
1310
1300
1311
-
traverse() {
1312
-
// ...
1301
+
getFeedback() {
1302
+
returnthis._feedback;
1303
+
}
1304
+
1305
+
// this method makes no sense to the Feedback subclass
1306
+
getRating() {
1307
+
returnthis._rating;
1308
+
}
1309
+
1310
+
// this method makes no sense to the Feedback subclass
1311
+
setRating(stars) {
1312
+
this._rating= stars;
1313
+
}
1314
+
1315
+
// this method makes no sense to the Feedback subclass
1316
+
postMessage(message) {
1317
+
console.log(message);
1313
1318
}
1314
1319
}
1315
1320
1316
-
const$=newDOMTraverser({
1317
-
rootNode:document.getElementsByTagName('body'),
1318
-
animationModule() {} // Most of the time, we won't need to animate when traversing.
0 commit comments