欢迎来到JS复习专栏,本文章主要内容是内置对象String和Math的常见方法
语法:
布尔值 = Number.isInteger(数字);
语法:
字符串 = myNum.toFixed(num);
将数字 myNum 的小数点后面保留 num 位小数(四舍五入),并返回。不会改变原数字。注意,返回结果是字符串。
举例:
let num = 3.456;
let num2 = num.toFixed(2);
console.log(num); // 打印结果:3.456
console.log(num2); // 打印结果:3.46
console.log(typeof num); // number
console.log(typeof num2); // string
下面这张图有简单的介绍

生成 [0, 1) 之间的随机浮点数。
//生成 [0, x) 之间的随机数
Math.round(Math.random()*x)
//生成 [x, y) 之间的随机数
Math.round(Math.random()*(y-x)+x)
生成 [x, y]之间的随机整数
我们可以直接封装一个函数,如下所示:
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
如果想计算 a 的 b 次方,可以使用如下函数:
Math.pow(a, b);
如果想计算数值a的开二次方,可以使用如下函数:
Math.sqrt(a);
URI (Uniform ResourceIdentifiers,通用资源标识符)进行编码,以便发送给浏览器。有效的URI中不能包含某些字符,例如空格。而这URI编码方法就可以对URI进行编码,它们用特殊的UTF-8编码替换所有无效的字符,从而让浏览器能够接受和理解。
encodeURIComponent(); 把字符串作为 URI 组件进行编码
decodeURIComponent(); 把字符串作为 URI 组件进行解码