5 基本引用类型
Date
常用方法
例如当前时间是:2022/5/3 18:29:13
方法名 | 说明 | 备注 |
---|---|---|
valueOf | 获取当前毫秒 | 从1970年1月1日0点开始计算,可计算至 285616 年后 |
getFullYear | 获取完整年份 | 2022 |
getMonth | 获取月份,以 0 开始 | 4 |
getDate | 获取日 | 3 |
getHours | 获取小时 0~24 | 18 |
getMinutes | 获取分钟 | 29 |
getSeconds | 获取秒 | 13 |
toLocaleString | 获取年月日 时分秒 | 2022/5/3 18:29:13 |
toLocaleDateString | 获取年月日 | 2022/5/3 |
toLocaleTimeString | 获取时分秒 | 18:29:13 |
// 实例化日期
const now = new Date()
console.log(now) // Tue May 03 2022 18:29:13 GMT+0800 (中国标准时间)
console.log(now.valueOf()) // 1651573753659
console.log(now.getFullYear()) // 2022
// ...
RegExp
常用的标记 flags
说明 | 备注 | |
---|---|---|
g | 全局模式 | global |
i | 不区分大小写 | ignoreCase |
// regExpDemo.js
const str = 'I have a cat, at home'
// 是否包含at
const reg1 = /at/g
// 是否包含 bat、cat
const reg2 = /[bc]at/i
// 是否包括以 at 结尾的三字符组合
const reg3 = /.at/gi
// 等效
const reg1 = /at/g
const reg2 = new RegExp('at', 'g')
常用元字符
正则斜杠开始,斜杠结尾,例如:/abc/
字符 | 说明 | 正则 | demo | 结果 |
---|---|---|---|---|
() | 完整匹配 | /(abc)d/ | abcd | true |
ad | false | |||
[] | 单个匹配 | /[abc]d/ | abcd | true |
ad | true | |||
dd | false | |||
. | 匹配任意字符(除\n、\r) | /.d/ | abcd | true |
=d | true | |||
d | false | |||
* | 匹配前面的子表达式零次或多次 | /abc*/ | abc | true |
abcd | true | |||
\ | 转义字符 |
// regCharacter.js
const str = 'abcdefg'
const reg1 = /(abc)d/
console.log(reg1.test(str)) // true
console.log(reg1.test('ad')) // false
const reg2 = /[abc]d/
console.log(reg2.test(str)) // true
console.log(reg2.test('ad')) // true
实例属性
// regProperty
const reg = /.at/gi
console.log(reg.source) // .at
console.log(reg.global) // true
console.log(reg.ignoreCase) // true
console.log(reg.flags) // gi
实例方法
// regExpDemo.js
test() // 返回一个布尔值
exec()
const str = 'abcdefg'
const reg = /[abc]d/
console.log(reg.exec(str)) // 输出如下
const res = {
0: "cd"
groups: undefined
index: 2
input: "abcdefg"
length: 1
}
原始值包装类型
如下代码,为什么 s1 明明是个原始数据类型,还能调用方法?
const s1 = 'hello, world'
const s2 = s1.substring(2)
// 当第 2 行访问 s1 时,是以读模式访问的,也就是要从内存中读取变量保存的值。以读模式访问字符串值的任何时候,后台都会执行以下三步:
//(1)创建一个 String 类型的实例
//(2)调用实例上的特定方法
//(3)销毁实例
这种行为可以让原始值拥有对象的行为。对布尔值和数值也是一样的。
数值常用的方法
const num = 1000
// 转换进制
num.toString(16) // 3e8
// 保留小数
num.toFixed(2) // 1000.00
// 科学计数法
num.toExponential() // 1e+3
// 根据情况返回合理的结果
num.toPrecision(3) // 1.00e+3
num.toPrecision(4) // 1000
num.toPrecision(6) // 1000.00
字符串常用的方法
可迭代 for-of,可解构
slice(index, index2)
substring(index, index2)
substr(index, index2)
indexOf(str, index)
lastIndexOf(str, index)
startsWith(str, index)
endsWith(str, index)
includes(str, index)
trim()
trimLeft()
trimRight()
repeat(number)
padStart(number, str)
padEnd(number, str)
'foo'.padStart(6, 'x') // xxxfoo
toLowerCase()
toUpperCase()
// match 等价于 RegExp 的 exec()
match(RegExp)
search(RegExp)
replace(str | RegExp, str)
split(str | RegExp, index)
'a,b,c,d'.split(',') // ['a', 'b', 'c', 'd']
'a,b,c,d'.split(',', 2) // ['a', 'b']
单例内置对象
Global
1、编码方式
encodeURI()
encodeURIComponent() // 会编码特殊字符,例如:冒号、斜杠、问号、井号
decodeURI()
decodeURIComponent()
2、eval
解释器,功能过于强大,易受 XSS 攻击,勿用。
3、window 对象
ECMA-262未规定直接访问 Global 对象的方式,但浏览器将 window 对象实现为 Global 对象的代理
Math
min(...nums)
max(...nums)
ceil(num) // 向上取整
floor(num) // 向下取整
round(num) // 四舍五入
random() // 获取 [0,1) 范围内的随机数
总结
1、JavaScript中的对象称为引用值。
- 引用值和类类似,但实现不同
- Date 类型提供日期、时间相关的消息及方法
- RegExp 类型是 ECMAScript 支持正则表达式的接口
2、JavaScript 比较独特的一点是,函数实际上是 Function 类型的实例,也就是说函数也是对象。
3、由于原始包装类型的存在,JavaScript 中的原始值也可以当作对象使用。包括:Boolea、Number 和 String,有以下特点:
- 每种包装类型都映射到同名的原始类型
- 以读模式访问原始值时,后台会实例化一个原始包装类型的对象,借助这个对象可以操作相应的数据
- 涉及原始值的语句执行完毕后,包装对象就会被销毁
4、代码开始执行时,全局上下文中会存在两个内置对象:Global 和 Math。其中,Global 对象在大多数 ECMAScript 实现中无法直接访问。不过浏览器将其实现为 window 对象。所有全局变量和函数都是 Global 对象的属性。Math 对象包含辅助完成复杂计算的属性和方法。