vue 源码 tostring

 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))

 

posted @ 2020-08-11 00:32  minfight  阅读(901)  评论(0)    收藏  举报