第五章:基本引用类型

第五章:基本引用类型

  • 引用值是某个特定引用类型的实例
    • 引用类型是把数据和功能组织到一起的结构,也称为“对象定义”
  • 对象被认为是某个特定引用类型的实例
    • 新对象通过 new 操作符后跟一个构造函数创建

5.1 Date

  • 时间起点:1970.1.1 00:00
  • Date.parse()接收表示日期的字符串,转换为毫秒数
  • Date.UTC()接收各单位的数字,转换为毫秒数
    • 年,月是必需的。日默认为 1 ,其他为 0
    • 1月为 0 ,以此类推
  • Date.now()返回当前时间的毫秒数

5.1.1 继承的方法

  • Date.toLocaleString()返回与浏览器运行的本地环境一致的日期和时间
  • Date.toString()返回带时区信息的日期和时间
  • Date.valueOf()返回日期的毫秒表示
    • Date 可以比较大小

5.1.2 日期格式化方法

  • Date.toDateString()显示星期、月、日、年
  • Date.toTimeString()显示时、分、秒、时区
  • Date.toLocaleDateString()显示星期、月、日、年(本地时间)
  • Date.toLocaleTimeString()显示时、分、秒(本地时间)
  • Date.toUTCString()显示完整的UTC日期

5.1.3 日期 / 时间组件方法

5.2 RegExp

  • 匹配模式标记
    • g:全局模式
    • i:不区分大小写
    • m:多行模式
    • y:粘附模式:只查找从 lastIndex 开始及以后的字符串(需配合 g使用)
    • u:Unicode 模式,启用 Unicode 匹配
    • s:dotAll 模式,表示元字符 . 匹配任何字符(包括 \n\r
  • 元字符:( [ { \ ^ $ | ) ] } ? * + .
    • 要匹配这些字符需要进行转义

5.2.1 RegExp 实例属性

5.2.2 RegExp 实例方法

  • 正则表达式的 toLocaleString()toString()返回其字面量
  • valueOf()返回正则表达式本身
exec()
  • 全局查找的模式下多次调用 exec()会向后继续查找
const text = "mom and dad and baby";
let pattern = /mom( and dad( and baby)?)?/gi;

let matches = pattern.exec(text);   // 返回包含 input 和 index 属性的数组,没找到则返回 null
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"
test()

5.2.3 RegExp 构造函数属性

  • 无 Web 标准出处,不要在生产环境中使用
全 名简 写说 明
input$_最后搜索的字符串
lastMatch$&最后匹配的文本
lastParen$+最后匹配的捕获组
leftContext$`input 字符串中出现在 lastMatch 前面的文本
rightContext$’input 字符串中出现在 lastMatch 后面的文本
const text = "this has been a short summer";
let pattern = /(.)hort/gi;

if (pattern.test(text)) {
    console.log(RegExp.input);          // "this has been a short summer"
    console.log(RegExp.leftContext);    // "this has been a "
    console.log(RegExp.rightContext);   // " summer"
    console.log(RegExp.lastMatch);      // "short"
    console.log(RegExp.lastParen);      // "s"
}
const text = "this has been a short summer";
let pattern = /(..)or(.)/gi;

if (pattern.test(text)) {
    console.log(RegExp.$1);     // "sh"
    console.log(RegExp.$2);     // "t"
                                // 最多存储 9 个
}

5.2.4 模式局限

5.3 原始值包装类型

let s1 = "some text";
let s2 = s1.substring(2);
//  s2 访问 s1 时以读模式访问
//  后台:  
//  1. 创建 String 类型实例
//  2. 调用实例上的特定方法
//  3. 销毁实例

5.3.1 Boolean

let falseObj = new Boolean(false);
let falseVal = false;

console.log(falseObj && true);              // true
console.log(falseVal && true);              // false

console.log(typeof falseObj);               // object
console.log(typeof falseVal);               // boolean
console.log(falseObj instanceof Boolean);   // true
console.log(falseVal instanceof Boolean);   // false

5.3.2 Number

  • toFixed():返回包含指定小数位的数值字符串(四舍五入)
    • 通常可以表示 0 - 20 个小数位
  • toExponential():返回科学计数法,同样可以指定小数位
  • toPrecision():返回最合理的输出结果,可以指定数字的总位数(不包含指数)
  • ES6 新增 Number.isInteger():判断是否为整数
    • 1.00 会判断为整数
  • Number.isSafeInteger():判断是否在二进制值可以表示的整数范围内
    • Number.MIN_SAFE_INTEGER: -2^53 + 1
    • Number.MAX_SAFE_INTEGER:2^53 - 1

5.3.3 String

  • 双字节字符的长度也按照单字节的长度计数
1. Javascript 字符
  • 由 16 位码元组成。对于多数字符,16 位码元对应一个字符。
  • charAt()
  • charCodeAt():返回指定位置的码元值
let message = "abcde";

// Unicode c = U+0063
console.log(message.charCodeAt(2));     // 99
// 十进制 99 等于 十六进制63
console.log(99 == 0x63);   // true
  • String.fromCharCode(): 根据给定的 UTF-16 码元创建字符
console.log(String.fromCharCode(0x61, 0x62, 0x63, 0x64, 0x65)); // abcde
  • 对于 Unicode 可以使用 charPointAt(), String.fromCharPoint()
2. normalize()方法
3. 字符串操作方法
  • slice()
  • substring()
  • substr()
4. 字符串位置方法
  • indexOf()
  • lastIndexOf()
5. 字符串包含方法(ES6 增加)
  • startsWith()
  • endsWith()
  • includes()
6. trim()方法
  • trimLeft()
  • trimRight()
7. repeat()方法
8. padStart()padEnd()方法
let str = "foo";

console.log(str.padStart(1));           // "foo"
console.log(str.padEnd(1));             // "foo"

console.log(str.padStart(6));           // "   foo"
console.log(str.padStart(6, "."));      // "...foo"

console.log(str.padEnd(6));             // "foo   "
console.log(str.padEnd(6, '.'));        // "foo..."
9. 字符串迭代与解构
  • 可以手动使用迭代器
const str = "abc";
const strIterator = str[Symbol.iterator]();

console.log(strIterator.next());    // { value: 'a', done: false }
console.log(strIterator.next());    // { value: 'b', done: false }
console.log(strIterator.next());    // { value: 'c', done: false }
console.log(strIterator.next());    // { value: undefined, done: true }
  • 使用 for-of
  • [...str]
10. 字符串大小写转换
  • toLowerCase()
  • toLocaleLowerCase():基于特殊地区实现
  • toUpperCase()
  • toLocaleUpperCase():基于特殊地区实现
11. 字符串模式匹配的方法
  • match():传入正则表达式,返回值与exec()相同
  • search():返回第一个匹配的位置索引
  • replace()
  • split()
12. localeCompare()方法
13. HTML 方法


5.4 单例内置对象

5.4.1 Global
  • 代码不能显示地访问
  1. URL 编码方法
  • encodeURI():对整个 URI 编码,如 "www.baidu.com/test.js"
    • 不会编码特殊符号,如冒号、斜杠、问号、井号
  • encodeURIComponent():编码 URI 中单独的组件,如 test.js
    • 会编码所有非标准字符
  • decodeURI()
  • decodeURIComponent()
  1. eval()方法
  • 通过该方法执行的代码属于当前上下文
  • 被执行代码拥有与上下文相同的作用域链
  • 内部定义的变量和函数不会被提升
  • 严格模式下,内部创建的变量和函数无法被外部访问
  • 面对恶意用户具有一定危险性
  1. Gloabl 对象属性

  1. window 对象
  • 详见第 12 章
  • 函数未明确指定 this 值的情况下(成为某个对象的方法,或通过 call() / apply()调用),this 值等于 Gloabl 对象
5.4.2 Math
  1. Math 对象属性
属 性说 明
Math.E自然对数的基数 e 的值
Math.LN10/
Math.LN2/
Math.LOG2E/
Math.LOG10E/
Math.PI/
Math.SQRT1_21/2 的平方根
Math.SQRT2/
  1. min()max()方法

  1. 舍入(取整)方法
  • Math.ceil():向上舍入(25.9 -> 26)
  • Math.florr():向下舍入
  • Math.round():四舍五入
  • Math.fround():返回最接近的单精度(32位)浮点值
  1. random()方法
  • 返回 0 - 1 范围内的随机数,包含 0,不包含 1
  • 加密等情况下建议使用 window.crypto.getRandomValues()
  • number = Math.floor(Math.random() * 可选总数 * 最小数)
  1. 其他方法
  • Math.abs():返回绝对值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值