JavaScript内部对象

  • 标准对象

    typeof 123 //"number"
    typeof '123' //"string"
    typeof true //"boolean"
    typeof NaN //"number"
    typeof [] //"object"
    typeof {} //"object"
    typeof Math.abs //"function"
    typeof undefined //"undefined"
    
  • Date

    var now = new Date();
    now.getFullYear(); //年
    now.getMonth(); //月 0-11
    now.getDate(); //日
    now.getDay(); //星期几
    now.getHours(); //时
    now.getMinutes(); //分
    now.getSeconds(); //秒
    now.getTime(); //时间戳,从1970-1-1 00:00:00开始到现在的msconsole.log(new Date(now.getTime())); //时间戳转为时间
    
    • 转换
    var now = new Date();
    now.toLocaleString(); //"2021/4/22下午4:12:56"
    now.toGMTString(); //"Thu, 22 Apr 2021 08:12:56 GMT"
    
  • JSON

    • 在JavaScript中一切皆为对象,任何js支持的类型都可以用JSON来表示

    • 格式:

      • 对象都用{}
      • 数组都用[]
      • 所有的键值对都用key:value
    • JSON字符串和JS对象的转化

    var user = {name : "li",age : 3,sex : '男'
    }//对象转化为json字符串
    var jsonUser = JSON.stringify(user); //{"name":"li","age":3,"sex":"男"}//json字符串转换为对象
    var obj = JSON.parse(jsonUser);