Finished Chapter 6.8 Serializing Objects

master
Jason Zhu 2021-01-12 20:59:10 +11:00
parent e381a49e37
commit ea7d183da6
1 changed files with 18 additions and 0 deletions

View File

@ -442,6 +442,24 @@ merge({x: 1}, {x: 2, y: 2}, {y: 3, z: 4}) // => {x: 1, y: 2, z: 4}
## 6.8 Serializing Objects
**Object serialization** = process to convert an object's state to a string. Then later we can restore it.
2 functions:
* `JSON.stringify()`: serialize JS objects.
* `JSON.parse`: restore JS objects.
JSON stands for **"JavaScript Object Notation"**
```js
let o = {x: 1, y: {z: [false, null, ""]}}; // Define a test object
let s = JSON.stringify(o); // s == '{"x":1,"y":{"z":[false,null,""]}}'
let p = JSON.parse(s); // p == {x: 1, y: {z: [false, null, ""]}}
```
What can/cannot be serialized?
* CAN be serialized: Objects, arrays, strings, finite numbers, `true`, `false`, `null`
* `NaN`, `Infinity`, `-Infinity` serialized to `null`
* CANNOT be serialized: Function, RegExp, Error objects, `undefined` value
## 6.9 Object Methods
### 6.9.1 The toString() Method