5. 内部对象
标准对象
typeof 123
"number"
typeof '123'
"string"
typeof true
"boolean"
typeof NaN
"number"
typeof []
"object"
typeof {}
"object"
typeof Math.abs
"function"
typeof undefined
"undefined"
5.1 Date
基本使用
var now = new Date(); //Sat May 21 2022 23:57:26 GMT+0800 (中国标准时间)
now.getFullYear(); // 年
now.getMonth(); // 月 0~11代表月
now.getDate(); // 日
now.getDay(); // 星期
now.getHours(); // 时
now.getMinutes(); // 分
now.getSeconds(); // 秒
now.getTime(); // 时间戳 格林威治时间1970年01月01日00时00分00秒起至当下的总秒数
console.log(new Date(1653149204809)); //时间戳转时间:VM121:1 Sun May 22 2022 00:06:44 GMT+0800 (中国标准时间)
now.toLocaleString(); //转换为当地时间:'2022/5/22 00:06:44'
now.toGMTString(); //转为GMT时间:'Sat, 21 May 2022 16:06:44 GMT
5.2 JSON
json是什么
早期,所有的数据传输习惯使用xml文件
- JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式
- 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。
- 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
在JavaScript 一切皆为对象、任何js支持的类型都可以用JSON来表示;
格式:
- 对象都用{}
- 数组都用[]
- 所有的键值对都使用 key : value
JSON字符串和JS对象转换
var user = {
name: 'Jack',
age: 3,
sex: '男'
}
// 对象转换为JSON字符串
var jsonUser = JSON.stringify(user); //{"name":"Jack","age":3,"sex":"男"}
// JSON字符串转换为对象
var obj = JSON.parse('{ "name": "Jack", "age": 3, "sex": "男" }');
5.3 Ajax
- 原生的JS写法 xhr 异步请求
- jQuery封装好的方法 $(“#name”).ajax(“”)
- axios 请求