JavaScript-internal objects (Date, JSON)

JavaScript-internal objects (Date, JSON)

Standard object

//返回对象类型
typeof 123
"number"//数字
typeof '神明'
"string"//字符串
typeof true
"boolean"//布尔值
typeof NaN
"number"
typeof Math
"object" //object
typeof Math.abs
"function"//函数
typeof [1,2,3]
"object"
typeof undefined
"undefined"//未定义
typeof {
    
    }
"object"

Date object

test

var now= new Date();//Sun Dec 20 2020 14:38:39 GMT+0800 (中国标准时间)
       now.getFullYear();//年
       now.getMonth();//月  0-11代表月
       now.getDate();//日
       now.getDay();//星期
       now.getHours();//小时
       now.getMinutes();//分
       now.getSeconds();//秒
       now.getTime()//时间戳
      

Insert picture description here

Conversion

 //通过时间戳返回当前时间
       console.log(new Date(1608499910969))
       // Mon Dec 21 2020 05:31:50 GMT+0800 (中国标准时间)
 
now = new Date(1608446910969)
Sun Dec 20 2020 14:48:30 GMT+0800 (中国标准时间)
now.toGMTString()//GMT时间
"Sun, 20 Dec 2020 06:48:30 GMT"
now.toISOString()//ISO时间
"2020-12-20T06:48:30.969Z"

JSON object

  • JSON ( JavaScript Object Notation, JS Object Notation) is a lightweight data exchange format

  • Concise and clearHierarchyMakes JSON an ideal data exchange language.

  • It is easy for people to read and write, but also easy for machine to parse and generate, and effectively improve network transmission efficiency.

Everything is an object in JavaScript, and any type supported by js can be represented by JSON

Representation format:

  • Object {}
  • Array with []
  • All key-value pairs use key: value

test

var user={
    
    
          name:"shenming",
          age:3,
          sex:"男"
        }
        //对象转换为JSON字符串
      var juser = JSON.stringify(user);
     
      //JSON字符串转换为对象,parse()参数为json字符串  JSON的字符串规定必须用双引号
     var obj = JSON.parse('{"name":"shenming","age":3,"sex":"男"}')

To turn any JavaScript object into JSON is to serialize this object into a JSON format string so that it can be transmitted to other computers via the network.
If we receive a string in JSON format, we only need to deserialize it into a JavaScript object, and then we can use this object directly in JavaScript.

Insert picture description here

Guess you like

Origin blog.csdn.net/wpc2018/article/details/111440534