长话简说JavaScript(5)Global和Math是什么东西?

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
ObjectObject 的构造函数
ArrayArray 的构造函数
FunctionFunction 的构造函数
BooleanBoolean 的构造函数
StringString 的构造函数
NumberNumber 的构造函数
DateDate 的构造函数
RegExpRegExp 的构造函数
SymbolSymbol的构造函数
ErrorError的构造函数
EvalErrorEvalError的构造函数
RangeErrorRangeError的构造函数
ReferenceErrorReferenceError的构造函数
SyntaxErrorSyntaxError的构造函数
TypeErrorTypeError的构造函数
URIErrorURIError的构造函数

Math
Math 对象作为保存数学公式、信息和计算的地方。
1.Math 对象属性

属性说明
Math.E自然对数的基数 e 的值
Math.LN1010 为底的自然对数
Math.LN22 为底的自然对数
Math.LOG2E以 2 为底 e 的对数
Math.LOG10E以 10 为底 e 的对数
Math.PIπ 的值
Math.SQRT1_21/2 的平方根
Math.SQRT22 的平方根

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 的正切
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听不见你的名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值