JSON.stringify

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

相关问题

如何判断一个函数是否为JavaScript原生函数

  1. Array.prototype.push.toString() //function push() { [native code] }
  2. function func(){}
  3. func.toString() // function func(){}

通过调用函数的toString方法,判断返回的字符串中是否包含 [native code]。

  1. // 借助于lodash
  2. _.isNative(Array.prototype.push);
  3. // => true
  4. _.isNative(_);
  5. // => false
  1. //lodash 的实现
  2. function isObject(value) {
  3. const type = typeof value
  4. return value != null && (type === 'object' || type === 'function')
  5. }
  6. const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  7. const reIsNative = RegExp(`^${
  8. Function.prototype.toString.call(Object.prototype.hasOwnProperty)
  9. .replace(reRegExpChar, '\\$&')
  10. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?')
  11. }$`);
  12. // reIsNative => /^function.*?\(\) \{ \[native code\] \}$/
  13. function isNative(value) {
  14. return isObject(value) && reIsNative.test(value)
  15. }
  16. export default isNative