文章目录
原始包装类型
ECMAScript提供了三种特殊的引用类型:Boolean、Number、String.每当使用到某个原始值的属性或方法的时候,后台都会创建一个相应原始包装类型的对象。
let s1="Amethyst";
let s2=s1.substring(2);//ethyst
原始值本身不是对象,按理来说没有方法,而这里却能够正常执行substring()是因为后台进行了处理:
- 创建一个String类型的实例
- 调用实例上的特定方法
- 销毁实例
但是原始包装对象只存在于访问它的那行代码执行期间,这也是与引用类型(离开作用域被销毁)的一个区别。
let s1="Amethyst";
s1.color="blueviolet";
console.log(s1.color);//undefined
区分原始值和原始包装对象:调用typeof
let value1=Number(25);
let value2=new Number(25);
console.log(typeof value1);//number
console.log(typeof value2);//object
Boolean
原始布尔值和Boolean对象(不要使用)之间的区别:利用typeof、instanceof
所有对象在布尔表达式中都会自动转换为true.
Number
与Boolean类型一样,Number也重写了valueOf()、toLocaleString()、toString()
valueOf()方法返回原始数值,其他两个返回数值字符串
toFixed()
接收一个参数,指定小数的位数(四舍五入),其自动舍入的特点可用于处理货币.
let value=10;
console.log(value.toFixed(2));//10.00
toPrecision()、toExponential()
科学计数法
toPrecision()接收一个参数表示结果中小数的位数,toExponential()接收要给参数表示结果中数字的总位数(不含指数)
let num=99;
console.log(num.toPrecision(1));//1e+2 四舍五入到100
console.log(num.toPrecision(2));//99
console.log(num.toPrecision(3));//99.0
console.log(num.toExponential(3));//9.900e+1
isInteger()与安全整数
isInteger()判断一个数值是否为整数
Number.isSafeInteger()判断一个数是否在安全范围内
数值范围:Number.MIN_SAFE_INTEGER(-2^53 +1)~Number.MAX_SAFE_INTEGER(2^53-1)
String
注意length是属性而不是方法
-
charAt() 返回给定索引位置的字符
-
charCodeAt() 返回指定索引位置的码元值
-
fromCharCode() 可传多个参数(UTF-16码元),返回所有参数对应的字符拼接起来的字符串
-
normalize() 规范化,参数:NFD、NFC、NFKD、NFKC
有的字符可以同一个BMP字符表示,也可以通过一个代理对表示字符串操作方法
-
concat 可接收多个参数,拼接字符串
-
slice()、substr()、substring() 提取子串
slice()和substring()的参数表示起止位置索引,substr()参数表示起始索引、字符个数
slice() 负数参数=>str.length+负参数
substring() 所有负参数=>0
substr() 第一个负参数=>str.length+负参数,第二个负参数=>0
左闭右开区间、substring(3,0)等价于substring(3,0)
let str='roses bloom';//length:11
console.log(str.slice(-3));//oom
console.log(str.substring(-3));//roses bloom
console.log(str.substr(-3));//oom
console.log(str.slice(3,-4));//es b
console.log(str.substring(3,-4));//ros
console.log(str.substr(3,-4));//""空字符串
字符串位置方法
- indexOf()、lastIndexOf() 从头、尾搜索字符,返回位置(没找到则返回-1)
字符串包含方法
-
startsWith()、endsWith()、includes()——返回布尔类型
startsWith() 检查开始于索引0的匹配项
endsWith()检查开始于(str.length-substring.length)的匹配项
includes()检查整个字符串
startsWith()和endsWith()可选第二个参数,表示开始搜索的位置,忽略该位置之前所有字符
let str="could roses bloom";
console.log(str.startsWith('roses'));//false
console.log(str.startsWith('could'));//true
console.log(str.endsWith('roses'));//false
console.log(str.endsWith('bloom'));//true
console.log(str.includes('ould'));//true
trim()去掉两端空格
操作的是副本,不会影响原始字符串
repeat() 复制字符串
接收一个整型参数,表示将字符串复制多少次
padStart()、padEnd() 填充字符
接收两个参数,长度和字符串
let str="could roses bloom";
//长度大于原始字符串=>填充字符
let str1=str.padStart(20,'h');//hhhcould roses bloom
let str2=str.padEnd(20,'h');//could roses bloomhhh
//长度小于或等于原始字符串=>返回原始字符串
let str3=str.padEnd(10,'h');//could roses bloom
字符串迭代与解构
字符串的原型上暴露了一个@@iterator方法,表示可以迭代字符串的每个字符
可以像这样手动使用迭代器
let str="abc";
let stringIterator=str[Symbol.iterator]();
console.log(stringIterator.next());//{ value: 'a', done: false }
console.log(stringIterator.next());//{ value: 'b', done: false }
console.log(stringIterator.next());//{ value: 'c', done: false }
console.log(stringIterator.next());//{ value: undefined, done: true }
在for-of循环中可以通过这个迭代器来按序访问每个字符
let str="abc";
for(var c of str){
console.log(c);
}
toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()
字符串大小写转换
字符串模式匹配方法
- match()
- search()
- replace()
RegExp对象的exec()方法返回一个数组,数组包含两个属性index和input
let matches=pattern.exec(str);
index:字符串匹配的起始位置
input:要查找的字符串
matches[0]:匹配的是整个模式的字符串,其他元素匹配表达式中的捕获组
lastIndex:非全局模式下,lastIndex的值始终不变,全局模式下返回上次匹配的最后一个字符的索引,如果设置了粘附标记y,则下一次以lastIndex处字符作为开头匹配
let str="bat,cat,sat,fat";
let pattern=/.at/g;
let matches=pattern.exec(str);
console.log(matches.index);//0
console.log(matches[0]);//bat
console.log(pattern.lastIndex);//3
matches=pattern.exec(str);
console.log(matches[0]);//cat
//添加粘附标记 pattern=/.at/gy
console.log(matches);//null
match()方法本质上跟RegExp对象的exec()方法相同,match()接收一个参数,可以是正则表达式,也可以是RegExp对象
let str="bat,cat,sat,fat";
let pattern=/.at/;
//等价于pattern.exec(str);
let matches=str.match(pattern);
console.log(matches.index);//0
console.log(matches[0]);//bat
console.log(pattern.lastIndex);//0
search() 返回第一个匹配位置的索引,没有则返回-1
replace() 查找替换
localeCompare() 比较字符串
区分大小写、按字典序
let str='abc';
let str1='bcd';
console.log(str.localeCompare(str1));//-1
单例内置对象
前面我们接触了内置对象Object、Array、String,现在我们介绍另外两个单例内置对象:Global、Math
Global
Global对象是一种兜底对象,它所针对的是不属于任何对象的属性和方法
在全局作用域中定义的变量和函数都会变成Global对象的属性
包括isNaN(),isFinite()、parseInt()等方法都是Global对象的方法
eval()
接收一个字符串(可执行)参数
定义在包含在上下文中的变量可以在eval()函数内部引用
let str="hello cloud";
eval('console.log(str)');//hello cloud
可以在eval()函数内部定义一个函数或变量,在外部代码中引用
eval('function sayHi(){console.log(\'Hi\')}');//Hi
sayHi();
这个地方sayHi()是在eval()内部定义的,因为该调用会被替换为真正的函数,所以可以在下面可以调用sayHi(),变量也一样
let str="hello cloud";
eval("message1='hello'");
console.log(message1);//hello
eval("let message2='hhhh'");
console.log(massage2);//报错
通过eval()定义的任何函数和变量都不会提升,只有eval()执行的时候才被创建
window对象
ECMA-262没有规定直接访问Global对象的方式,但是浏览器将window对象实现为Global对象的代理
调用一个简单返回this的函数是在任何执行上下文中获取Global对象的通用方式
Math对象
Math对象包含辅助完成复杂计算的属性和方法
console.log(Math.random()*10+1);//从1开始,能取十个数