Skip to content

Commit 0887afc

Browse files
committed
docs(stdlib/JSON): add JSON.parse(JSON.stringify(obj))
1 parent 482a517 commit 0887afc

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

docs/stdlib/json.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@ JSON.parse('{"a": 1, "b": 2}', f)
395395

396396
上面代码中,`JSON.parse()`的第二个参数是一个函数,如果键名是`a`,该函数会将键值加上10。
397397

398+
`JSON.parse()``JSON.stringify()`可以结合使用,像下面这样写,实现对象的深拷贝。
399+
400+
```javascript
401+
JSON.parse(JSON.stringify(obj))
402+
```
403+
404+
上面这种写法,可以深度克隆一个对象,但是对象内部不能有 JSON
405+
不允许的数据类型,比如函数、正则对象、日期对象等。
406+
398407
## 参考链接
399408

400409
- MDN, [Using native JSON](https://developer.mozilla.org/en-US/docs/Using_native_JSON)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"homepage": "https://github.com/wangdoc/javascript-tutorial",
3737
"dependencies": {
38-
"gh-pages": "^3.2.3",
38+
"gh-pages": "5.x",
3939
"husky": "^4.3.8",
4040
"loppo": "^0.6.24",
4141
"loppo-theme-wangdoc": "^0.6.1"

0 commit comments

Comments
 (0)