Global
一种兜底对象,在全局作用域中定义的变量和函数都会变成 Global 对象的属性 。包括 isNaN()、isFinite()、parseInt()和 parseFloat(),实际上都是 Global 对象的方法。
1.URL 编码方法
encodeURI()和 encodeURIComponent()方法用于编码统一资源标识符(URI),以便传给浏览器。有效的 URI 不能包含某些字符,比如空格。使用 URI 编码方法来编码 URI 可以让浏览器能够理解它们,同时又以特殊的 UTF-8 编码替换掉所有无效字符。
这两个方法的主要区别是,encodeURI()不会编码属于 URL 组件的特殊字符,比如冒号、斜杠、问号、井号,而 encodeURIComponent()会编码它发现的所有非标准字符。
let uri = "https://www.runoob.com/regexp/regexp-tutorial.html";
// https://www.runoob.com/regexp/regexp-tutorial.html
console.log(encodeURI(uri));
// https%3A%2F%2Fwww.runoob.com%2Fregexp%2Fregexp-tutorial.html
console.log(encodeURIComponent(uri));
2.eval()方法
是一个完整的 ECMAScript 解释器。用户输入内容时,这个方法会对 XSS 利用暴露出很大的攻击面,导致网站崩溃。
eval("console.log('hi')"); // hi
console.log('hi')// hi
3.Global 对象属性
属性 | 说明 |
---|---|
undefined | 特殊值 undefined |
NaN | 特殊值 NaN |
Infinity | 特殊值 Infinity |
Object | Object 的构造函数 |
Array | Array 的构造函数 |
Function | Function 的构造函数 |
Boolean | Boolean 的构造函数 |
String | String 的构造函数 |
Number | Number 的构造函数 |
Date | Date 的构造函数 |
RegExp | RegExp 的构造函数 |
Symbol | Symbol的构造函数 |
Error | Error的构造函数 |
EvalError | EvalError的构造函数 |
RangeError | RangeError的构造函数 |
ReferenceError | ReferenceError的构造函数 |
SyntaxError | SyntaxError的构造函数 |
TypeError | TypeError的构造函数 |
URIError | URIError的构造函数 |
Math
Math 对象作为保存数学公式、信息和计算的地方。
1.Math 对象属性
属性 | 说明 |
---|---|
Math.E | 自然对数的基数 e 的值 |
Math.LN10 | 10 为底的自然对数 |
Math.LN2 | 2 为底的自然对数 |
Math.LOG2E | 以 2 为底 e 的对数 |
Math.LOG10E | 以 10 为底 e 的对数 |
Math.PI | π 的值 |
Math.SQRT1_2 | 1/2 的平方根 |
Math.SQRT2 | 2 的平方根 |
2.min()和 max()方法
let max = Math.max(3, 54, 32, 16);
console.log(max); // 54 最大值
let min = Math.min(3, 54, 32, 16);
console.log(min); // 3 最小值
3.舍入方法
console.log(Math.ceil(25.9)); // 26 始终向上舍入为最接近的整数。
console.log(Math.round(25.9)); // 26 始终向下舍入为最接近的整数。
console.log(Math.fround(25.9)); // 25.899999618530273 执行四舍五入。
console.log(Math.floor(25.9)); // 25 返回数值最接近的单精度(32 位)浮点值表示。
4.random()方法
Math.random()方法返回一个 0~1 范围内的随机数,其中包含 0 但不包含 1。
console.log( Math.floor(Math.random() * 10 + 1)); // 2 从 1~10 范围内随机选择一个数
5.其他方法
涉及各种简单或高阶数运算的方法。
方 法 | 说 明 |
---|---|
Math.abs(x) | 返回 x 的绝对值 |
Math.abs(x) | 返回 x 的绝对值 |
Math.exp(x) | 返回 Math.E 的 x 次幂 |
Math.expm1(x) | 等于 Math.exp(x) - 1 |
Math.log(x) | 返回 x 的自然对数 |
Math.log1p(x) | 等于 1 + Math.log(x) |
Math.pow(x, power) | 返回 x 的 power 次幂 |
Math.hypot(…nums) | 返回 nums 中每个数平方和的平方根 |
Math.clz32(x) | 返回 32 位整数 x 的前置零的数量 |
Math.sign(x) | 返回表示 x 符号的 1、0、-0 或-1 |
Math.trunc(x) | 返回 x 的整数部分,删除所有小数 |
Math.sqrt(x) | 返回 x 的平方根 |
Math.cbrt(x) | 返回 x 的立方根 |
Math.acos(x) | 返回 x 的反余弦 |
Math.acosh(x) | 返回 x 的反双曲余弦 |
Math.asin(x) | 返回 x 的反正弦 |
Math.atan(x) | 返回 x 的反正切 |
Math.atanh(x) | 返回 x 的反双曲正切 |
Math.trunc(x) | 返回 x 的整数部分,删除所有小数 |
Math.atan2(y, x) | 返回 y/x 的反正切 |
Math.cos(x) | 返回 x 的余弦 |
Math.sin(x) | 返回 x 的正弦 |
Math.tan(x) | 返回 x 的正切 |