File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -559,12 +559,26 @@ const DEFAULTS = {
559
559
560
560
function processContent (options ) {
561
561
options = Object .assign ({}, DEFAULTS , options);
562
+ // ...
562
563
}
563
564
```
564
565
565
566
上面代码中,` DEFAULTS ` 对象是默认值,` options ` 对象是用户提供的参数。` Object.assign ` 方法将` DEFAULTS ` 和` options ` 合并成一个新对象,如果两者有同名属性,则` option ` 的属性值会覆盖` DEFAULTS ` 的属性值。
566
567
567
- 注意,由于存在深拷贝的问题,` DEFAULTS ` 对象和` options ` 对象的所有属性的值,都只能是简单类型,而不能指向另一个对象。否则,将导致` DEFAULTS ` 对象的该属性不起作用。
568
+ 注意,由于存在深拷贝的问题,` DEFAULTS ` 对象和` options ` 对象的所有属性的值,最好都是简单类型,不要指向另一个对象。否则,` DEFAULTS ` 对象的该属性很可能不起作用。
569
+
570
+ ``` javascript
571
+ const DEFAULTS = {
572
+ url: {
573
+ host: ' example.com' ,
574
+ port: 7070
575
+ },
576
+ };
577
+
578
+ processContent ({ url: {port: 8000 } })
579
+ ```
580
+
581
+ 上面代码中,原意是将` url.port ` 改成8000,` url.host ` 不变。实际结果却是` options.url ` 覆盖掉` DEFAULTS.url ` ,` url.host ` 不存在了。
568
582
569
583
## 属性的可枚举性
570
584
You can’t perform that action at this time.
0 commit comments