Skip to content
Spotify - 每月低于 10 元

第10章 内置对象

Number

类型转换

  • Number 可以将字符串转换为数字
  • Number.parseInt() 转化为整数 Number.pareseFloat() 转换为浮点数
  • 当字符串为非数字时,转换为 NaN
js
let strNum1 = '15';
let strNum2 = '15.5';
let strNum3 = '十五点五';

let num1 = Number.parseInt(strNum1); // 15
let num2 = Number.parseFloat(strNum2); // 15.5
let num3 = Number.parseInt(strNum3); // NaN

保留位数

  • toFixed() 方法可用于保留位数
  • 返回值为 string 类型
js
let num = 3.1415926535;
let numStr = num.toFixed(4); // 3.1416
console.log(typeof numStr); // string

Math

最值

  • Math.max() 与 Math.min() 可以求最大值与最小值
  • 含非数字时 返回 NaN
  • 无参数返回时 返回-Infinity
js
Math.max(1, 3, 5); // 5
Math.max(-1, -3, -5); // -1
Math.max(1, 3, 'five'); // NaN
Math.max(); // -Infinity

Math.min(1, 3, 5); // 1
Math.min(-1, -3, -5); // -5
Math.min(1, 3, 'five'); // NaN
Math.min(); // -Infinity

绝对值

  • Math.abs() 可以求最大值与最小值
  • 当值为 string 类型时,隐式转换为 number
js
Math.abs(-1); // 1
Math.abs('-1'); // 1

取整

  • Math.floor() 向下取整
js
Math.floor(1.1); // 1
Math.floor(1.9); // 1
  • Math.ceil() 向上取整
js
Math.ceil(1.1); // 2
Math.ceil(1.9); // 2
  • Math.trunc() 去除小数部分
js
Math.round(1.1); // 1
Math.round(1.5); // 2
Math.round(1.9); // 2
Math.round(-1.1); // -1
Math.round(-1.5); // -1 特殊情况:遇到 .5时往大取
  • Math.round() 四舍五入取整
js
Math.trunc(5.123); // 5

生成随机数

  • Math.random() 会返回一个随机小数 [0, 1)
js
// 得到两数之间的随机整数
function getRandomIntInclusive(min, max) {
  return Math.floor(Math.random() * (max - min)) + min; // 不含最大值,含最小值
}

function getRandomIntInclusive(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min; // 含最大值,含最小值
}

其他常见属性及方法

js
Math.PI; // 3.141592653589793
Math.sin(Math.PI / 2); // 1
Math.pow(10, 3); // 1000

Date

创建 Date 对象

  • 不含参数 返回当前系统时间
  • 含参数 数字型 2022, 01, 01 字符串型 '2022-01-01 00:00:00'
js
let date1 = new Date(); // 返回当前系统时间
let date2 = new Date(2022, 01, 01); // 返回2月 不是1月
let date3 = new Date('2022-01-01 00:00:00'); // 返回1月

日期格式化

js
let date = new Date();
date.getFullYear(); // 返回当前年
date.getMonth() + 1; // 返回当前月份小1月 加1后为当前月份 (0-11)
date.getDate(); // 返回当前日
date.getDay(); // 返回星期几 (周日为0 周六为6) (常用arr数组输出)
date.getHours(); // 返回当前小时
date.getMinutes(); // 返回当前分钟
date.getSeconds(); // 返回当前秒

获取总毫秒

  • 获取距离 1970-01-01 的总毫秒数
  • 方式一:date.valueOf()
  • 方式二:date.getTime()
  • 方式三:直接转换为 number 类型
  • 方式四:Date.now()
js
let date = new Date();
date.valueOf();	// 方式一
date.getTime(); // 方式二
+date; // 方式三
Date.now(); // 方式四

JSON

  • JSON 字符串中的 key 需要用双引号包裹
  • JSON.parse() 方法可将 JSON 字符串转化为 JavaScript对象
  • JSON.stringify() 方法可将 JavaScript 对象转换为 JSON 字符串
js
let postJSON = `{
  "id": 1,
  "title": "标题",
  "comments": [
      { "userId": 1, "comment": "评论1" }
  ],
  "published": true,
  "author": null
}`;

let JSObject = JSON.parse(postJSON);
let postJSON2 = JSON.stringify(JSObject);

Set

概念

  • Set 是一种没有重复元素的集合
  • Set 也可用于添加对象作为元素
  • Set 判断是否是同一个对象判断的是内存地址,不是对象的内容

添加元素

js
let set = new Set();

set.add(1);
set.add(3);
set.add(5);
set.add(3);
console.log(set); // Set(3) { 1, 3, 5 }

判断元素

js
set.has(5); // true

遍历 Set

js
set.forEach((ele) => {
  console.log(ele);
});

删除元素

js
set.delete(3);
console.log(set); // Set(3) { 1, 5 }

清空集合

js
set.clear();
console.log(set); // Set(0) {}

Map

概念

  • Map 是键值对的数据结构
  • key 和 value 可以为任意类型

添加键值对

js
let map = new Map();
let objKey = { key: 2 };

map.set(1, '值1');
map.set(objKey, '值2');
map.set('key 3', '值3');
console.log(map); // Map(3) {1 => '值1', {…} => '值2', 'key 3' => '值3'}

获取 Value

js
map.get(1); // 值1
map.get(objKey); // 值2
map.get('key 3'); // 值3

判断元素

js
map.has('key 3'); // true

删除元素

js
map.delete(1);
console.log(map); // Map(2) { { key: 2 } => '值2', 'key 3' => '值3' }

遍历 Map

  • 方式一:forEach 回调函数

    • 第一个参数为 value 第二个参数为 key
  • 方式二:for...of 返回一个数组

    • 数组第一项为 key 数组第二项为 value
js
map.forEach((value, key) => {
  console.log(key, value);
});

for (let [key, value] of map) {
  console.log(key, value);
}
关注微信公众号V.PS- 美国 CN2 GIA / 9929 / CMIN2 顶级线路
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0

预览:

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3