首页 > 其他 > 详细

vue 源码 tostring

时间:2020-08-11 09:08:14      阅读:78      评论:0      收藏:0      [点我收藏+]
 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))

 

vue 源码 tostring

原文:https://www.cnblogs.com/minfight/p/13473169.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!