内置对象
方法和说明:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects
Date 日期对象
var now = new Date(); //获取当前计算机的日期时间
now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
now.getFullYear(); // 2015, 年份
now.getUTCFullYear() 返回 UTC 日期的 4 位数年
now.getMonth(); // 5, 月份,注意月份范围是0~11,5表示六月
now.setMonth(month);// 设置日期
now.getDate(); // 24, 表示24号
now.setDate(date) 设置日期中的日(如果 date 大于该月天数,则加月)
now.getDay(); // 3, 表示星期三,0 表示周日,6 表示周六
now.getHours(); // 19, 24小时制
now.getMinutes(); // 49, 分钟
now.getSeconds(); // 22, 秒
now.getMilliseconds(); // 875, 毫秒数
now.getTime(); // 1435146562875, 以number形式表示的时间戳
var d = new Date(2015, 5, 19, 20, 15, 30, 123); //注意这里的5是6月
d; // Fri Jun 19 2015 20:15:30 GMT+0800 (CST)
var d = Date.parse('2015-06-24T19:49:22.875+08:00');//还可以是 “月/日/年”,如"5/23/2019" 或者 “月名 日, 年”,如"May 23, 2019";
d; // 1435146562875
时间戳是一个自增的整数,它表示从1970年1月1日零时整的GMT时区开始的那一刻,到现在的毫秒数。
假设浏览器所在电脑的时间是准确的,那么世界上无论哪个时区的电脑,它们此刻产生的时间戳数字都是一样的,所以,时间戳可以精确地表示一个时刻,并且与时区无关。
日期格式化
Date 类型有几个专门用于格式化日期的方法,它们都会返回字符串:
toDateString()显示日期中的周几、月、日、年(格式特定于实现);
toTimeString()显示日期中的时、分、秒和时区(格式特定于实现);
toLocaleDateString()显示日期中的周几、月、日、年(格式特定于实现和地区);
toLocaleTimeString()显示日期中的时、分、秒(格式特定于实现和地区);
toUTCString()显示完整的 UTC 日期(格式特定于实现)。
计算代码执行时间
// 起始时间
let start = Date.now();
// 调用函数
doSomething();
// 结束时间
let stop = Date.now(),
result = stop - start;
Math 数学对象
和数学计算有关,使用时不用new,直接Math().方法
四舍五入
Math.ceil()方法始终向上舍入为最接近的整数。
Math.floor()方法始终向下舍入为最接近的整数。
Math.round()方法执行四舍五入。
Math.fround()方法返回数值最接近的单精度(32 位)浮点值表示
随机
Math.random(); //方法返回一个 0~1 范围内的随机数,其中包含 0 但不包含 1
let num = Math.floor(Math.random() * 10 + 1); //这样就有 10 个可能的值(1~10),其中最小的值是 1
加密(建议)
如果是为了加密而需要生成随机数(传给生成器的输入需要较高的不确定性),那么建议使用 window.crypto. getRandomValues( 数组参数 )
数组需要是一个基于整数的 TypedArray,它可以是 Int8Array、Uint8Array、Int16Array、 Uint16Array、 Int32Array 或者 Uint32Array 这种定型数组。
var array = new Uint32Array(1); // 1个元素
window.crypto.getRandomValues(array); // Uint32Array [1142501950] 随机的
定型数组可以查看:
JS - 数组 Array
Array 数组对象
数组的操作方法,也可以用来操作对象
ES7
返回true、false
indexOf 是返回下标或者-1
ES10
多维数组转低维
参数是深度,默认只转换1层
Number 数值对象
保留x位小数
let num = 10;
console.log(num.toFixed(2)); // "10.00" 表示返回的数值字符串要包含两位小数
console.log(num.toExponential(1)); // "1.0e+1"科学记数法
String 字符串对象
let s1 = "some text";
let s2 = s1.substring(2);
// 相当于
let s1 = new String("some text");
let s2 = s1.substring(2);
s1 = null;
str.concat 拼接
let stringValue = "hello ";
let result = stringValue.concat("world");
console.log(result); // "hello world"
let result1 = stringValue + "world";
console.log(result1); // "hello world"
大小写转换
let stringValue = "hello world";
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLowerCase()); // "hello world"
提取
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w" 从哪个位置提取到哪个位置
console.log(stringValue.substring(3,7)); // "lo w" 从哪个位置提取到哪个位置
console.log(stringValue.substr(3, 7)); // "lo worl" 从哪个位置提取几位
console.log(stringValue.slice(-3)); // "rld"
console.log(stringValue.substring(-3)); // "hello world"
console.log(stringValue.substr(-3)); // "rld"
console.log(stringValue.slice(3, -4)); // "lo w"
console.log(stringValue.substring(3, -4)); // "hel"
console.log(stringValue.substr(3, -4)); // "" (empty string)
… 解构
let message = "abcde";
console.log([...message]); // ["a", "b", "c", "d", "e"]
包含
let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("bar")); // false
console.log(message.startsWith("foo", 1)); // false 1是搜索位置,从第1个字符开始搜索
console.log(message.endsWith("baz")); // true
console.log(message.endsWith("bar")); // false
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false
console.log(message.includes("bar", 4)); // false 4是搜索位置,从第4个字符开始搜索
str.trimStart 清除空白(ES10)
清除空白
console.log(str.trim()); // 清除前后的空格
str.matchAll (ES11)
有个可迭代的next方法
1、遍历获取
2、扩展运算符
Global
Global 对象是 ECMAScript 中最特别的对象,因为代码不会显式地访问它。
包括 isNaN()、isFinite()、parseInt()和 parseFloat(),实际上都是 Global 对象的方法。
像 undefined、NaN 和 Infinity 等特殊值都是 Global 对象的属性。
此外,所有原生引用类型构造函数,比如 Object 和 Function,也都是Global 对象的属性。
ES11 globalThis 绝对全局对象
可以用于获取当前环境的全局对象,比如浏览器就是window
nodejs环境是一个全局对象 global
此时可以统一使用 globalThis 这个对象,在浏览器环境就是window,在nodejs环境就是global对象
URL 编码
let uri = "http://www.wrox.com/illegal value.js#start";
console.log(encodeURI(uri)); // "http://www.wrox.com/illegal%20value.js#start"
console.log(encodeURIComponent(uri)); // "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start"
eval 方法(不推荐使用)
解释代码字符串的能力是非常强大的,但也非常危险。
在使用 eval()的时候必须极为慎重,特别是在解释用户输入的内容时。
因为这个方法会对 XSS 利用暴露出很大的攻击面。恶意用户可能插入会导致你网站或应用崩溃的代码。
通常比其他替代方法更慢,因为它必须调用 JS 解释器
let msg = "hello world";
// 同时拥有上下文
eval("console.log(msg)");
// 等价于
console.log(msg);
eval("function sayHi() { console.log('hi'); }");
sayHi();
替代方案 window.Function
let msg = "hello world";
Function('"use strict";console.log(msg);')();
window 对象
浏览器将 window 对象实现为 Global对象的代理。
因此,所有全局作用域中声明的变量和函数都变成了 window 的属性。
====================
宿主对象
不是JS定义的对象,而是js所在环境定义的,比如浏览器中,浏览器的元素对象就是宿主对象