toString() 与 JSON.stringify()

1. toString

In addition to the custom toString, call Object.prototype.toString object, it returns the object internal property [[Class]] => "[obejct Obejct]", custom toString, as serialized array Array.prototype.toString use "," link.

Object.prototype.toString

let obj = {a: '333'}

Object.prototype.toString.call(obj)

// "[object Object]"

 

Array.prototype.toString

let arr = [1,  2,  3]
Array.prototype.toString.call(arr)
// "1,2,3"

 

Equivalent to

arr.join(",")

 

2. JSON.stringify()

JSON.stringify (value, replacer, space) to a value (object or array) is converted to a JSON string, it first calls the object itself toJSON ().

obj= {toJSON: function() {return '24';}}

JSON.stringify(obj)

// ""24""

 

JSON.stringify, replacer receiving array, or function, specifies which properties can be processed, which may be filtered. Return undefined attributes are filtered.

JSON.stringify({a: 1, b: 'zzz', c: 'xxx'}, ["a", "c"])
// "{"a":1,"c":"xxx"}"

 

When the number of space, a specified number of each indentation, when the string, one for each indentation with a string 10 before replacement.

 

Guess you like

Origin www.cnblogs.com/xiabaoying/p/12107381.html