3.基本引用类型
3.1 Date
let now = new Date();
在不给 Date 构造函数传参数的情况下,创建的对象将保存当前日期和时间。
3.2 RegExp
第一种方式是直接通过/正则表达式/
写出来,第二种方式是通过new RegExp('正则表达式')
创建一个RegExp对象。
3.2.1 exec()
除了简单地判断是否匹配之外,正则表达式还有提取子串的强大功能。用()
表示的就是要提取的分组(Group)。RegExp 实例的主要方法是 exec(),主要用于配合捕获组使用。这个方法只接收一个参数,即要应 用模式的字符串。如果找到了匹配项,则返回包含第一个匹配信息的数组;如果没找到匹配项,则返回 null。返回的数组虽然是 Array 的实例,但包含两个额外的属性:index 和 input。index 是字符串 中匹配模式的起始位置,input 是要查找的字符串。
let text = "mom and dad and baby";
let pattern = /mom( and dad( and baby)?)?/gi;
let matches = pattern.exec(text);
console.log(matches.index); // 0
console.log(matches.input); // "mom and dad and baby"
console.log(matches[0]); // "mom and dad and baby"
console.log(matches[1]); // " and dad and baby"
console.log(matches[2]); // " and baby"
3.3原始值包装类型
ECMAScript 提供了 3 种特殊的引用类型:Boolean、Number 和 String。 这些类型具有本章介绍的其他引用类型一样的特点,但也具有与各自原始类型对应的特殊行为。
每当用 到某个原始值的方法或属性时,后台都会创建一个相应原始包装类型的对象,从而暴露出操作原始值的 各种方法。
//例子
let s1 = "some text";
let s2 = s1.substring(2);
//1.创建一个String类型的实例
//2.调用实例上的特定方法
//3.销毁实例
引用类型与原始值包装类型的主要区别在于对象的生命周期。在通过 new 实例化引用类型后,得到 的实例会在离开作用域时被销毁,而自动创建的原始值包装对象则只存在于访问它的那行代码执行期 间。这意味着不能在运行时给原始值添加属性和方法。
3.3.1 Boolean
let falseObject = new Boolean(false);//所有对象在布尔表达式都会转换为true
let result = falseObject && true;
console.log(result); // true
let falseValue = false;
result = falseValue && true; //
console.log(result); // false
所有对象在布 尔表达式中都会自动转换为 true。
3.3.2 Number
toFixed()
let num = 10;
console.log(num.toFixed(2)); // "10.00"
let num = 10.005;
console.log(num.toFixed(2)); // "10.01"
这里的 toFixed()方法接收了参数 2,表示返回的数值字符串要包含两位小数。结果返回值为 “10.00”,小数位填充了 0。如果数值本身的小数位超过了参数指定的位数,则四舍五入到最接近的小数位
typeof and instanceof
let numberObject = new Number(10);
let numberValue = 10; //原始值没有创建实例的说法
console.log(typeof numberObject); // "object"
console.log(typeof numberValue); // "number"
console.log(numberObject instanceof Number); // true
console.log(numberValue instanceof Number); // false
boolean值也存在相应的情况
3.3.3 Sring
字符串操作方法
concat() 连接字符串
concat(),用于将一个或多个字符串拼接成一个新字符串
let stringValue = "hello ";
let result = stringValue.concat("world");
console.log(result); // "hello world"
console.log(stringValue); // "hello"
slice()、substr()和 substring() 分离出子串
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7)); // "lo worl"
indexOf()和 lastIndexOf() 子串定位
这两个方法从字符 串中搜索传入的字符串,并返回位置(如果没找到,则返回-1)。两者的区别在于,indexOf()方法 从字符串开头开始查找子字符串,而 lastIndexOf()方法从字符串末尾开始查找子字符串。
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.lastIndexOf("o")); // 7
//第二个参数为可选参数,从第几个位置开始查找
let stringValue = "hello world";
console.log(stringValue.indexOf("o", 6)); // 7
console.log(stringValue.lastIndexOf("o", 6)); // 4
startsWith()、 endsWith()和 includes() 字符串包含
这些方法都会从字符串中搜索传入的字符串,并返回一个表示是否包含 的布尔值。它们的区别在于,startsWith()检查开始于索引 0 的匹配项,endsWith()检查开始于索 引(string.length - substring.length)的匹配项,而 includes()检查整个字符串
let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("bar")); // false
console.log(message.endsWith("baz")); // true
console.log(message.endsWith("bar")); // false
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false
//第二个为可选参数,表示从第几个数开始查找,索引从0开始
let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("foo", 1)); // false
console.log(message.includes("bar")); // true
console.log(message.includes("bar", 4)); // false
trim() 删除空格
这个方法会创建字符串的一个副本,删除前、 后所有空格符,再返回结果,原本字符串不受影响
另外,trimLeft()和 trimRight()方法分别用于从字符串开始和末尾清理空格符。
let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"
repeat() 字符串复制
这个方法接收一个整数参数,表示要将字 符串复制多少次,然后返回拼接所有副本后的结果。
let stringValue = "na ";
console.log(stringValue.repeat(16) + "batman");
// na na na na na na na na na na na na na na na na batman
padStart()和 padEnd()方法
padStart()和 padEnd()方法会复制字符串,如果小于指定长度,则在相应一边填充字符,直至 满足长度条件。这两个方法的第一个参数是长度,第二个参数是可选的填充字符串,默认为空格
let stringValue = "foo";
console.log(stringValue.padStart(6)); // " foo"
console.log(stringValue.padStart(9, ".")); // "......foo"
console.log(stringValue.padEnd(6)); // "foo "
console.log(stringValue.padEnd(9, ".")); // "foo......"
for-of
for (const c of "abcde") {
console.log(c);
}
// a
// b
// c
// d
// e
//将有了这个迭代器之后,字符串就可以通过解构操作符来解构了。比如,可以更方便地把字符串分割为字符数组
let message = "abcde";
console.log([...message]); // ["a", "b", "c", "d", "e"]
字符串大小写
toLowerCase()、toLocaleLowerCase()、toUpperCase()和toLocaleUpperCase()
toLocaleLowerCase()和 toLocaleUpperCase()方法旨在基于 特定地区实现。在很多地区,地区特定的方法与通用的方法是一样的。但在少数语言中(如土耳其语), Unicode 大小写转换需应用特殊规则,要使用地区特定的方法才能实现正确转换。
let stringValue = "hello world";
console.log(stringValue.toLocaleUpperCase()); // "HELLO WORLD"
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLocaleLowerCase()); // "hello world"
console.log(stringValue.toLowerCase()); // "hello world"
字符串模式匹配方法
match(),这个方 法本质上跟 RegExp 对象的 exec()方法相同。match()方法接收一个参数,可以是一个正则表达式字 符串,也可以是一个 RegExp 对象。match()方法返回的数组与 RegExp 对象的 exec()方法返回的数组是一样的:第一个元素是与整 个模式匹配的字符串,其余元素则是与表达式中的捕获组匹配的字符串(如果有的话)
let text = "cat, bat, sat, fat";
let pattern = /.at/;
// 等价于 pattern.exec(text)
let matches = text.match(pattern);
console.log(matches.index); // 0
console.log(matches[0]); // "cat"
console.log(pattern.lastIndex); // 0
search()。这个方法唯一的参数与 match()方法一样:正则表达 式字符串或 RegExp 对象。这个方法返回模式第一个匹配的位置索引,如果没找到则返回1。
let text = "cat, bat, sat, fat";
let pos = text.search(/at/);
console.log(pos); // 1
replace()方法。这个方法接收两个参数,第一个 参数可以是一个 RegExp 对象或一个字符串(这个字符串不会转换为正则表达式),第二个参数可以是 一个字符串或一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字 符串,第一个参数必须为正则表达式并且带全局标记
let text = "cat, bat, sat, fat";
let result = text.replace("at", "ond");
console.log(result); // "cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
console.log(result); // "cond, bond, sond, fond"
3.4 单例内置对象
3.4.1 Global
Global 对象是 ECMAScript 中最特别的对象,因为代码不会显式地访问它。在全局作用域中定义的变量和函数都会变成 Global 对象的属性 。
虽然 ECMA-262 没有规定直接访问 Global 对象的方式,但浏览器将 window 对象实现为 Global 对象的代理。因此,所有全局作用域中声明的变量和函数都变成了 window 的属性。
另一种获取 Global 对象的方式是使用如下的代码: let global = function() { return this; }();
3.4.2 Math
min()和 max()
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
四舍五入
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
console.log(Math.round(25.9)); // 26
console.log(Math.round(25.5)); // 26
console.log(Math.round(25.1)); // 25
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
console.log(Math.floor(25.5)); // 25
console.log(Math.floor(25.1)); // 25
random()
Math.random()方法返回一个 0~1 范围内的随机数,其中包含 0 但不包含 1
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
为了加密而需要 生成随机数(传给生成器的输入需要较高的不确定性),那么建议使用 window.crypto. getRandomValues()