var _toString = Object.prototype.toString; /** * Strict object type check. Only returns true * for plain JavaScript objects. */ function isPlainObject(obj) { return _toString.call(obj) === ‘[object Object]‘ } function toString(val) { return val == null ? ‘‘ : Array.isArray(val) || (isPlainObject(val) && val.toString === _toString) ? JSON.stringify(val, null, 2) : String(val) } // JSON.stringify() 方法将一个 JavaScript 对象或值转换为 JSON 字符串, // String(val) // String 函数将其他值生成或转换成字符串 var o = { name: "mimin", age: "17" } var st1 = " hello word" console.log(isPlainObject(o))//true console.log(isPlainObject(st1))//false console.log(st1.toString) console.log(o.toString === _toString)//true console.log(JSON.stringify(o, null, 2)) console.log(JSON.stringify(o, null, 8))
原文:https://www.cnblogs.com/minfight/p/13473169.html