File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -1379,21 +1379,31 @@ renderLargeShapes(shapes);
1379
1379
```
1380
1380
** [ ⬆ 返回顶部] ( #代码整洁的-javascript ) **
1381
1381
1382
- ### Interface Segregation Principle (ISP)
1382
+ ### 接口隔离原则 (ISP)
1383
1383
JavaScript doesn't have interfaces so this principle doesn't apply as strictly
1384
1384
as others. However, it's important and relevant even with JavaScript's lack of
1385
1385
type system.
1386
1386
1387
+ JavaScript 没有接口, 所以这个原则不想其它语言那么严格。 不过, 对于 JavaScript 这种缺少类
1388
+ 型的语言来说, 它依然是重要并且有意义的。
1389
+
1387
1390
ISP states that "Clients should not be forced to depend upon interfaces that
1388
1391
they do not use." Interfaces are implicit contracts in JavaScript because of
1389
1392
duck typing.
1390
1393
1394
+ 接口隔离原则说的是 “客户端不应该强制依赖他们不需要的接口。” 在 JavaScript 这种弱类型语言中,
1395
+ 接口是隐式的契约。
1396
+
1391
1397
A good example to look at that demonstrates this principle in JavaScript is for
1392
1398
classes that require large settings objects. Not requiring clients to setup
1393
1399
huge amounts of options is beneficial, because most of the time they won't need
1394
1400
all of the settings. Making them optional helps prevent having a "fat interface".
1395
1401
1396
- ** Bad:**
1402
+ 在 JavaScript 中能比较好的说明这个原则的是一个类需要一个巨大的配置对象。 不需要客户端去设置大
1403
+ 量的选项是有益的, 因为多数情况下他们不需要全部的设置。 让它们变成可选的有助于防止出现一个“胖接
1404
+ 口”。
1405
+
1406
+ ** 不好的:**
1397
1407
``` javascript
1398
1408
class DOMTraverser {
1399
1409
constructor (settings ) {
@@ -1419,7 +1429,7 @@ const $ = new DOMTraverser({
1419
1429
1420
1430
```
1421
1431
1422
- ** Good ** :
1432
+ ** 好的: **
1423
1433
``` javascript
1424
1434
class DOMTraverser {
1425
1435
constructor (settings ) {
You can’t perform that action at this time.
0 commit comments