目录
自动装箱
1. 原始值与包装对象的区别
2. 自动装箱的触发场景
3. 自动装箱的底层机制
4. 显式包装 vs 自动装箱
String 实例方法和静态方法
一、实例属性
1. length
二、实例方法
2. split(separator[, limit])
3. substring(startIndex[, endIndex])
4. startsWith(searchString[, position])
5. includes(searchString[, position])
6. toUpperCase()
7. toLowerCase()
8. indexOf(searchValue[, fromIndex])
9. endsWith(searchString[, length])
10. replace(searchValue, replaceValue)
11. match(regexp)
三、String 作为函数使用
强制转换为字符串
Number 静态方法和实例方法
一、静态方法
1. Number.isNaN(value)
2. Number.isFinite(value)
3. Number.isInteger(value)
4. Number.parseFloat(string) / Number.parseInt(string, radix)
5. Number.EPSILON
6. Number.MAX_SAFE_INTEGER / Number.MIN_SAFE_INTEGER
二、实例方法
1. toFixed(digits)
2. toExponential(fractionDigits)
3. toPrecision(precision)
4. toString(radix)
5. valueOf()
在 JavaScript 中,自动装箱(Autoboxing) 是一种隐式机制,使得原始值(如字符串、数字、布尔值)能够临时转换为对应的 包装对象(如
String
、Number
、Boolean
),从而允许开发者直接调用对象方法。这一过程由 JavaScript 引擎自动完成,无需开发者显式操作。
自动装箱
1. 原始值与包装对象的区别
类型 | 示例 | 特性 |
---|---|---|
原始值 | "hello" , 42 | 无属性和方法,存储在栈内存 |
包装对象 | new String("hello") | 是对象,有属性和方法,存储在堆内存 |
2. 自动装箱的触发场景
当尝试在原始值上访问属性或调用方法时,JavaScript 引擎会 临时创建包装对象,操作完成后立即销毁该对象。
示例:
const str = "hello";
console.log(str.toUpperCase()); // "HELLO"// 引擎隐式执行以下步骤:
// 1. 创建临时 String 对象:const tempStr = new String(str);
// 2. 调用方法:tempStr.toUpperCase();
// 3. 销毁 tempStr
3. 自动装箱的底层机制
-
步骤:
- 创建临时对象:根据原始值类型创建对应的包装对象(如
String
、Number
)。 - 执行操作:调用方法或访问属性。
- 销毁对象:操作完成后,临时对象被垃圾回收。
-
内存模型:
原始值 "hello"(栈内存)│└─→ 临时 String 对象(堆内存)├─ 方法:toUpperCase()└─ __proto__: String.prototype
4. 显式包装 vs 自动装箱
行为 | 显式包装(不推荐) | 自动装箱(推荐) |
---|---|---|
代码示例 | const str = new String("hello") | const str = "hello" |
类型 | typeof str → "object" | typeof str → "string" |
内存开销 | 长期占用堆内存 | 临时对象快速销毁 |
使用场景 | 特殊需求(如扩展原型方法) | 常规操作(如调用 toUpperCase() ) |
String 实例方法和静态方法
一、实例属性
1. length
-
作用:获取字符串的字符数量。
-
语法:
str.length
-
返回值:数值(字符串长度)。
-
示例:
const str = "hello"; console.log(str.length); // 5
二、实例方法
2. split(separator[, limit])
-
作用:按分隔符将字符串拆分为数组。
-
语法:
str.split(separator, limit)
-
参数:
-
separator
:分隔符(字符串或正则表达式)。 -
limit
(可选):限制返回数组的长度。
-
-
返回值:分割后的数组。
-
示例:
const str = "a,b,c"; console.log(str.split(",")); // ["a", "b", "c"] console.log(str.split(",", 2)); // ["a", "b"]
3. substring(startIndex[, endIndex])
-
作用:截取子字符串(不包含结束索引)。
-
语法:
str.substring(startIndex, endIndex)
-
参数:
-
startIndex
:起始索引(包含)。 -
endIndex
(可选):结束索引(不包含),默认到字符串末尾。
-
-
返回值:截取的新字符串。
-
示例:
const str = "hello world"; console.log(str.substring(0, 5)); // "hello" console.log(str.substring(6)); // "world"
4. startsWith(searchString[, position])
-
作用:检测字符串是否以指定子串开头。
-
语法:
str.startsWith(searchString, position)
-
参数:
-
searchString
:要搜索的子串。 -
position
(可选):开始搜索的位置(默认0
)。
-
-
返回值:布尔值。
-
示例:
const str = "hello world"; console.log(str.startsWith("hello")); // true console.log(str.startsWith("world", 6)); // true
5. includes(searchString[, position])
-
作用:判断字符串是否包含指定子串。
-
语法:
str.includes(searchString, position)
-
参数:
-
searchString
:要搜索的子串。 -
position
(可选):开始搜索的位置(默认0
)。
-
-
返回值:布尔值。
-
示例:
const str = "hello world"; console.log(str.includes("world")); // true
6. toUpperCase()
-
作用:将字符串转换为大写。
-
语法:
str.toUpperCase()
-
返回值:新字符串(全大写)。
-
示例:
const str = "Hello"; console.log(str.toUpperCase()); // "HELLO"
7. toLowerCase()
-
作用:将字符串转换为小写。
-
语法:
str.toLowerCase()
-
返回值:新字符串(全小写)。
-
示例:
const str = "HELLO"; console.log(str.toLowerCase()); // "hello"
8. indexOf(searchValue[, fromIndex])
-
作用:返回子串首次出现的索引(未找到返回
-1
)。 -
语法:
str.indexOf(searchValue, fromIndex)
-
参数:
-
searchValue
:要搜索的子串。 -
fromIndex
(可选):开始搜索的位置(默认0
)。
-
-
返回值:索引值(数值)。
-
示例:
const str = "hello world"; console.log(str.indexOf("world")); // 6
9. endsWith(searchString[, length])
-
作用:检测字符串是否以指定子串结尾。
-
语法:
str.endsWith(searchString, length)
-
参数:
-
searchString
:要搜索的子串。 -
length
(可选):作为字符串长度(默认完整长度)。
-
-
返回值:布尔值。
-
示例:
const str = "hello world"; console.log(str.endsWith("world")); // true
10. replace(searchValue, replaceValue)
-
作用:替换字符串中的子串(支持正则)。
-
语法:
str.replace(searchValue, replaceValue)
-
参数:
-
searchValue
:要替换的子串或正则表达式。 -
replaceValue
:替换内容(字符串或函数)。
-
-
返回值:新字符串(原字符串不变)。
-
示例:
const str = "hello world"; console.log(str.replace("world", "JS")); // "hello JS" // 正则替换 console.log("2023-01-01".replace(/-/g, "/")); // "2023/01/01"
11. match(regexp)
-
作用:返回字符串匹配正则的结果。
-
语法:
str.match(regexp)
-
参数:
-
regexp
:正则表达式。
-
-
返回值:
-
匹配成功:返回数组(含匹配信息)。
-
匹配失败:返回
null
。
-
-
示例:
const str = "hello 2023"; console.log(str.match(/\d+/)); // ["2023", index: 6, ...]
三、String 作为函数使用
强制转换为字符串
-
语法:
String(value)
-
作用:将任意值转换为字符串类型。
-
示例:
console.log(String(42)); // "42" console.log(String(null)); // "null" console.log(String({})); // "[object Object]"
Number
静态方法和实例方法
一、静态方法
1. Number.isNaN(value)
-
作用:检测值是否为
NaN
(严格模式,不会强制转换类型)。 -
参数:
value
(待检测的值)。 -
返回值:布尔值。
-
示例:
console.log(Number.isNaN(NaN)); // true console.log(Number.isNaN("NaN")); // false(字符串非 NaN) console.log(isNaN("123")); // false(全局 isNaN 会先转换类型)
2. Number.isFinite(value)
-
作用:检测值是否为有限数字(非
Infinity
/-Infinity
/NaN
)。 -
参数:
value
(待检测的值)。 -
返回值:布尔值。
-
示例:
console.log(Number.isFinite(42)); // true console.log(Number.isFinite(Infinity)); // false
3. Number.isInteger(value)
-
作用:检测值是否为整数(忽略小数部分)。
-
参数:
value
(待检测的值)。 -
返回值:布尔值。
-
示例:
console.log(Number.isInteger(5.0)); // true console.log(Number.isInteger(5.1)); // false
4. Number.parseFloat(string)
/ Number.parseInt(string, radix)
-
作用:将字符串解析为浮点数或整数(等价于全局
parseFloat
/parseInt
)。 -
参数:
-
string
:要解析的字符串。 -
radix
(parseInt
可选):进制基数(2-36)。
-
-
返回值:解析后的数值(无法解析返回
NaN
)。 -
示例:
console.log(Number.parseFloat("12.3px")); // 12.3 console.log(Number.parseInt("1010", 2)); // 10(二进制解析)
5. Number.EPSILON
-
作用:表示数值的最小精度(用于浮点数比较)。
-
示例:
// 避免浮点运算误差 console.log(0.1 + 0.2 === 0.3); // false console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true
6. Number.MAX_SAFE_INTEGER
/ Number.MIN_SAFE_INTEGER
-
作用:表示 JavaScript 能精确表示的最大/最小安全整数(
±(2^53 - 1)
)。 -
示例:
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
二、实例方法
1. toFixed(digits)
-
作用:将数值格式化为指定小数位数的字符串(四舍五入)。
-
参数:
-
digits
:小数位数(0-20)。
-
-
返回值:字符串。
-
示例:
const num = 12.345; console.log(num.toFixed(2)); // "12.35" console.log(12.toFixed(2)); // 语法错误,需写成 12..toFixed(2)
2. toExponential(fractionDigits)
-
作用:将数值转换为科学计数法表示的字符串。
-
参数:
-
fractionDigits
:小数位数(0-20)。
-
-
返回值:字符串。
-
示例:
const num = 1234.5; console.log(num.toExponential(2)); // "1.23e+3"
3. toPrecision(precision)
-
作用:根据指定精度格式化数值(自动选择定点或科学计数法)。
-
参数:
-
precision
:有效数字位数(1-21)。
-
-
返回值:字符串。
-
示例:
const num = 123.456; console.log(num.toPrecision(4)); // "123.5" console.log(num.toPrecision(2)); // "1.2e+2"
4. toString(radix)
-
作用:将数值转换为指定进制的字符串。
-
参数:
-
radix
(可选):进制基数(2-36),默认十进制。
-
-
返回值:字符串。
-
示例:
const num = 15; console.log(num.toString(2)); // "1111" console.log(num.toString(16)); // "f"
5. valueOf()
-
作用:返回数值的原始值(通常直接返回数值本身)。
-
返回值:数值。
-
示例:
const numObj = new Number(42); console.log(numObj.valueOf()); // 42