diff --git a/notes/chap6_objects.md b/notes/chap6_objects.md index 84a4289..91f5016 100644 --- a/notes/chap6_objects.md +++ b/notes/chap6_objects.md @@ -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