基本引用类型
The Global Object
Global object
Global object
- 无法显式访问;
- 所有可以被全局访问的变量和函数为其属性;
属性
| 属性 | 描述 | 属性 | 描述 |
|---|---|---|---|
| undefined | The special value undefined | RegExp | Constructor for RegExp |
| NaN | The special value wes | Symbol | Pseudo-constructor for Symbol |
| Infinity | The special value Infinity | Error | Constructor for Error |
| Object | Constructor for object | EvalError | Constructor for EvalError |
| Array | Constructor for Array | RangeError | Constructor for RangeError |
| Function | Constructor for Function | ReferenceError | Constructor for ReferenceError |
| Boolean | Constructor for Boolean | SyntaxError | Constructor for syntaxError |
| String | Constructor for string | TypeError | Constructor for TypeError |
| Number | Constructor for Number | URIError | Constructor for URIError |
| Date | Constructor for Date |
Window Object
Window Object
- 浏览器中 global object 的代理;
- 所有全局作用域的变量和函数都成为 window 的属性;
获取 Global object 的一种方法
- 非严格模式, 普通函数未指定 this 时指向 Global object;
let global = (function () {
return this;
})();
函数属性
eval() 方法
- eval(string);
- 执行 string 表示的代码;
- 如同 string 表示的代码插入到当前位置;
- 具有 eval() 函数被调用位置的 execute context;
- 非严格模式, eval() 中的函数和方法可以被外界调用;
- eval() 中的变量和函数不会被提升;
- eval() 执行完毕后即销毁;
数学相关
- isFinite(value): 检查参数是否为有限数值, 是则返回 true, 否则返回 false;
- isNaN(value): 检查参数是否为 NaN, 是则返回 true, 否则返回 false;
- parseFloat(string): 解析字符串为浮点数;
- parseInt(string, radix? : number | undefined): 解析字符串为整数;
编码相关
- encodeURI(URI): 将 URI 编码为对应 UTF-8 encoding;
- encodeURIComponent(URI): 将部分 URI 编码为对应 UTF-8 encoding;
- decodeURI(URI): 解析 decodeURI() 创建的 URI;
- decodeURIComponent(URI): 解析 decodeURIComponent() 创建的 URI;
不同
- encodeURIComponent() 和 decodeURIComponent 会对更多的字符进行转义, 例如
:,/,?,#,@,&,=,+,$,%等;
The Date Type
基本概念
- 毫秒级时间戳;
let now = new Date();
常用 API
Date() 构造函数
- Date(parameters) 构造函数: 根据 parameters 创建本地 Date object;
- Date.parse(dateString) 静态方法: 返回 dateString 对应的毫秒级时间戳;
- Date.UTC(year, month, day, hour, second, milliseconds) 静态方法: 返回对应的毫秒级时间戳;
- Date.now() 静态方法: 返回当前事件的毫秒级时间戳;
- Date.prototype.toXXX(): 输出 Date 为本地时区/本地语言形式/世界时字符串;
- Date.prototype.getXXX/setXXX(): 获取/设置本地时间/世界时;
- Date.prototype.valueOf(): 同 Date.prototype.getTime() 方法;
THE REGEXP TYPE
语法格式
let expression = /pattern/afgls;
let expression = new RegExp(pattern, flags);
匹配模式
// g 匹配全部内容
let pattern1 = /at/g;
// i 忽略大小写
let pattern1 = /at/i;
// m 多行模式
let pattern1 = /at/m;
// y 仅匹配 lastIndex 之后的字符串
let pattern1 = /at/y;
// u 使用 unicode 匹配
let pattern1 = /at/u;
// s 表示 . 匹配任何字符, 包括换行符
let pattern1 = /at/s;
// d 表示返回匹配字符串的起始索引和结束索引
let pattern1 = /at/d;
// 匹配模式组合
let pattern1 = /at/gi;
实例属性
let pattern = /\[bc\]at/i;
pattern.global; // 是否设置 g 标记
pattern.ignoreCase; // 是否设置 i 标记
pattern.unicode; // 是否设置 u 标记
pattern.sticky; // 是否设置 y 标记
pattern.dotAll; // 是否设置 s 标记
pattern.multiline; // 是否设置 m 标记
pattern.hasIndices; // 是否设置 d 标记
pattern.source; // 正则表达式的字面量字符串, \[bc\]at
pattern.flags; // 正则表达式的标记字符串, i
pattern.lastIndex; // 源字符串搜索的起始位置
实例方法
- RegExp.prototype.exec(str): 返回第一个匹配的字符串信息的数组, 反之为 null;
- index: 匹配字符串的起始位置;
- input: 源字符串;
- matches: 匹配的字符串数组, n;
- RegExp.prototype.test(str): 验证 str 是否与模式匹配, 匹配返回 true, 反之为 false;
- RegExp.escape(string): 转义 string, 返回一个安全的用于 RegExp 的字符串;
/g 标记
- 设置 g 标记后, 多次调用, 在上次基础上, 返回下一个匹配数组;
let text = "cat, bat, sat, fat";
let pattern = /.at/g;
let matches = pattern.exec(text);
console.log(matches.index); // 0
matches = pattern.exec(text);
console.log(matches.index); // 5
The Math Object
静态属性
| 属性 | 描述 |
|---|---|
| Math.E | e |
| Math.LN10 | ln10 |
| Math.LN2 | ln2 |
| Math.LOG10E | |
| Math.LOG2E | |
| Math.PI | PI |
| Math.SQRT1_2 | |
| Math.SQRT2 |
常用 API
- Math.min([value0, value1, ..., valueN]) 静态方法: 返回最小值;
- Math.max([value0, value1, ..., valueN]) 静态方法: 返回最大值;
- Math.floor(x) 静态方法: 向下取整;
- Math.trunc(x) 静态方法: 向下取整;
- 比 Math.floor() 函数更快更简单;
- Math.ceil(x): 静态方法: 向上取整;
- Math.round(x) 静态方法: 返回最近的整数值;
- Math.fround(doubleFloat)静态方法: 返回最近 x 的单精度浮点值;
- Math.random() 静态方法: 返回 [0, 1) 中的随机数;
- Math.abs(x)静态方法: 计算绝对值;
- Math.pow(base, exponent)静态方法: 计算 base 为底数, exponent 为指数的指数值;
- Math.exp(x) 静态方法: 计算 e 为底数的指数值;
- Math.log(x)静态方法: 计算 e 为底数的对数值;
- Math.log2(x)静态方法: 计算 2 为底数的对数值;
- Math.log10(x)静态方法: 计算 10 为底数的对数值;
- Math.sqrt(x) 静态方法: 计算平方根;
- Math.cbrt(x) 静态方法: 计算立方根;
- Math.hypot([value0, value1, ..., valueN]) 静态方法: 计算所有参数平方的和的平方根;
- 避免 sqrt() 方法极大极小值溢出的问题;
- Math.sign(x) 静态方法: 返回 x 符号;
- Math.trigonometric_functions(x): 各种三角函数运算, 使用弧度值;
- sin()/cos()/tan();
- asin()/acos()/atan();
- sinh()/cosh()/tanh();
- asinh()/acosh()/atanh();
原始包装类型
基本概念
primitive wrapper types
- Boolean;
- Number;
- String;
机制
- 当 primitive value 被读取时;
- 临时生成一个对应的 primitive wrapper types 的实例;
- 调用其实例方法, 使用后销毁;
reference types 和 primitive wrapper types 的区别
- 声明周期不同;
- reference types: 跳出所在 scope 之前;
- primitive wrapper types: 仅一行;
显式创建 primitive wrapper types
- 使用 new 关键字 和 Boolean(), Number() 和 String() 构造函数;
- 使用 Object(arg) 构造函数;
- arg;
- string: 视为 String();
- number: 视为 Number();
- Boolean: 视为 Boolean();
- arg;
- 若无必须, 不推荐使用;
The Boolean Type
逻辑运算
// Boolean 参与逻辑运算, 作为 object 参与;
let falseObject = new Boolean(false);
let result = falseObject && true;
console.log(result); // true
常用 API
- Boolean(value: any): boolean 构造函数: 返回 value 对应的布尔值;
- Boolean.prototype.valueOf(): boolean: 返回 false 或 true;
- Boolean.prototype.toString(): string: 返回 "false" 或 "true";
The Number Type
常用 API
- Number(value? : any): number 构造函数: 转换 value 为数字;
- Number.prototype.valueOf(): number: 返回对应数字形式;
- Number.prototype.toString(): string: 返回对应数字的字符串形式;
- Number.prototype.toFixed(digits: number =0): string: 返回保留 digits 为小数的字符串;
- Number.prototype.toExponential(): string: 同 toFixed() 方法, 不过为科学计数法形式;
- Number.prototype.toPrecision(): string: 根据数字大小选择 toFixed() 或 toExponential() 方法;
- Number.isInteger(number: number): boolean 静态方法: 判断 number 是否为 int 类型;
- Number.isSafeInteger(number: number): boolean 静态方法: 判断 number 是否为 int 类型且在安全取值范围内;
- Number.isNaN(number: number): boolean 静态方法: 判断 number 是否为非数字;
- Number.isFinite(number: number): boolean: 静态方法: 判断 number 是否为有限值;
- Number.parseInt(string: string, radix? : number | undefined): number 静态方法: 转换 string 为对应进制的整数形式;
- Number.parseFloat(string: string): number 静态方法: 转换 string 为对应进制的浮点数形式;
The String Type
code unit
- 字符串基本单元, 16 bit;
- 范围由 U+0000 到 U+FFFF (65535);
code point
- 由一个或者两个 16 bit code units (代理对) 构成;
- 对于两个 16 bit code units 构成的代理对;
- length 属性, charAt(), charCodeAt() 方法依旧将代理对按照两个分离的 16 bit code units 处理;
- fromCharCode(), codePointAt(), fromCodePoint() 合并处理;
常用 API
- String(value? : any): string 构造函数: 转换 value 为字符串;
- String.length: number 属性: 返回字符串 code unit 数量;
- String.fromCharCode(...codes: number[]): string: 静态方法, 根据 codes unit 显示对应字符串;
- String.fromCodePoint(...codePoints: number[]): string: 静态方法, 根据 code point 显示对应字符串;
- String.raw(callSite: TemplateStringsArray, ...substitutions: any[]): string: 静态方法, 返回模板字符串, 标签函数;
- String.prototype.at(index: number): string: 返回 index 对应字符, 支持负数索引;
- String.prototype.charAt(pos: number): string: 返回 pos 对应字符;
- String.prototype.charCodeAt(index: number): number: 返回 pos 对应字符的 code unit;
- String.prototype.charPointAt(index: number): number: 返回 pos 对应字符的 code point;
- String.prototype.concat(...strings: string[]): string: 链接字符串;
- String.prototype.endsWith(searchString: string, endPosition? : number | undefined): boolean: 是否以 endsWith 结尾;
- String.prototype.includes(searchString: string, position? : number | undefined): boolean: 是否具有 searchString 子串;
- String.prototype.indexOf(searchString: string, position? : number | undefined): number: searchString 首次出现时的首个字符索引值;
- String.prototype.isWellFormed(): boolean: 是否包含任一孤立代理对;
- String.prototype.lastIndexOf(searchString: string, position? : number | undefined): number: searchString 末次出现时的首个字符索引值;
- String.prototype.localeCompare(that: string): number: 根据字母表比较两个字符串排列顺序;
- String.prototype.match(regexp: string | RegExp): RegExpMatchArray | null 方法;
- String.prototype.matchAll(regexp: RegExp): IterableIteratorRegExpMatchArray 方法;
- String.prototype.normalize(form? : string | undefined): string: 标准化字符串;
- String.prototype.padEnd(maxLength: number, fillString? : string | undefined): string: 填补字符串结尾直至字符串到达指定长度;
- String.prototype.padStart(maxLength: number, fillString? : string | undefined): string: 填补字符串开头直至字符串到达指定长度;
- String.prototype.repeat(count: number): string: 重复链接字符串 count 次;
- String.prototype.replace(searchValue: string | RegExp, replaceValue: string): string: 替换字符串;
- String.prototype.replaceAll(searchValue: string | RegExp, replaceValue: string): string: 替换所有字符串;
- String.prototype.search(regexp: string | RegExp): number: 返回 regexp 首次出现时的首个字符索引值;
- String.prototype.slice(start? : number | undefined, end? : number | undefined): string: 取 [indexStart, indexEnd) 切片;
- 可使用负数索引;
- String.prototype.split(separator: string | RegExp, limit? : number | undefined): string[]: 分割字符串;
- String.prototype.startsWith(searchString: string, position? : number | undefined): boolean: 是否以 searchString 开头;
- String.prototype.substring(start: number, end? : number | undefined): string: 取 [indexStart, indexEnd) 切片;
- String.prototype.toLowerCase(): string: 转换为小写形式;
- String.prototype.toString(): string: 返回对应字符串形式;
- String.prototype.toUpperCase(): string: 转换为大写形式;
- String.prototype.trim(): string: 去除字符串空白;
- String.prototype.trimEnd(): 去除字符串末尾空白;
- String.prototype.trimStart(): string: 去除字符串开头空白;
- String.prototype.valueOf(): string: 返回对应字符串形式;
最佳实践
取任意范围内的整数
function selectFrom(lowerValue, upperValue) {
let choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}