“任何由ECMAScript实现提供、与宿主环境无关,并在ECMAScript程序开始执行时就存在的对象”。包括Object、Array 、String、Global 、Math等。
一、Global
Global 对象是ECMAScript 中最特别的对象,因为
代码不会显式地访问它
。ECMA-262 规定Global
对象为一种
兜底对象
,它
所针对的是不属于任何对象的属性和方法
。在
全局作用域中定义的变量和函数都会变成 Global 对象的属性
。包括 isNaN()、isFinite()、parseInt()和 parseFloat(),实际上都是 Global 对象的方法。除了这些,Global 对象上还有另外一些方法。
1. URL 编码方法 encodeURI()、encodeURIComponent()、decodeURI()和decodeURI-
Component()
使用URI 编码方法来编码URI 可以让浏览器能够理解它们,同时又以特殊的UTF-8 编码替换掉所有无效字符。
let uri = "http://www.wrox.com/illegal value.js#start";
// "http://www.wrox.com/illegal%20value.js#start"
console.log(encodeURI(uri));
// "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start"
console.log(encodeURIComponent(uri));
let uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start";
// http%3A%2F%2Fwww.wrox.com%2Fillegal value.js%23start
console.log(decodeURI(uri));
// http:// www.wrox.com/illegal value.js#start
console.log(decodeURIComponent(uri));
2. eval()方法
这个方法就是一个完整的 ECMAScript 解释器,它接收一个参数,即一个要执行的 ECMAScript(JavaScript)字符串。
eval("console.log('hi')");
//等价于
console.log("hi");
在使用 eval()的时候必须极为慎重,特别是在解释用户输入的内容时。因为这个方法会对 XSS 利用暴露出很大的攻击面。恶意用户可能插入会导致你网站或应用崩溃的代码。
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 的构造函数
4. window 对象
浏览器将 window 对象实现为 Global 对象的代理。因此,所有全局作用域中声明的变量和函数都变成了 window 的属性。
var color = "red";
7
function sayColor() {
console.log(window.color);
}
8
window.sayColor(); // "red"
调用一个简单返回 this 的函数是在任何执行上下文中获取 Global 对象的通用方式。
let global = function() {
return this;
}();
二、Math
Math 对象上提供的计算要比直接在JavaScript 实现的快得多。但使用 Math 计算的问题是精
度会因浏览器、操作系统、指令集和硬件而异。
1.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
2.舍入方法 Math.ceil()、Math.floor()、Math.round()和 Math.fround()
Math.ceil()方法始终向上舍入为最接近的整数。
Math.floor()方法始终向下舍入为最接近的整数。
Math.round()方法执行四舍五入。
Math.fround()方法返回数值最接近的单精度(32 位)浮点值表示。
console.log(Math.ceil(25.9)); // 26
console.log(Math.ceil(25.5)); // 26
console.log(Math.ceil(25.1)); // 26
3
console.log(Math.round(25.9)); // 26
console.log(Math.round(25.5)); // 26
console.log(Math.round(25.1)); // 25
4
console.log(Math.fround(0.4)); // 0.4000000059604645
console.log(Math.fround(0.5)); // 0.5
console.log(Math.fround(25.9)); // 25.899999618530273
console.log(Math.floor(25.9)); // 25 5
console.log(Math.floor(25.5)); // 25
console.log(Math.floor(25.1)); // 25
3.random()方法
返回一个0~1 范围内的随机数,其中包含0 但不包含1。Math.random()始终返回小数,即便乘以一个数再加上一个数也是小数,若依需要有配合Math.floor()方法使用。
let num = Math.random() * 10 + 1
console.log(num) //7.668849757363433
let num = Math.random() * 10 + 1
console.log(num) //10.565100110340447
let num = Math.floor(Math.random() * 10 + 1);
console.log(num) //9
如果想选择一个2~10 范围内的值
let num = Math.floor(Math.random() * 9 + 2);
*9是因为可选总数是9,人为计算可选总数比较麻烦,可以使用函数计算可选总数和最小可能的值
function selectFrom(lowerValue, upperValue) {
let choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}
let num = selectFrom(2,10);
console.log(num); // 2~10 范围内的值,其中包含2 和10
随机选择数组中的一项
function selectFrom(lowerValue, upperValue) {
let choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}
let colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
let color = colors[selectFrom(0, colors.length-1)];
如果是为了加密而需要生成随机数(传给生成器的输入需要较高的不确定性),那么建议使用 window.crypto. getRandomValues()。