包装对象的实例方法

课后整理 2020-12-20

包装对象实例可以使用Object对象提供的原生方法,主要是valueOf方法和toString方法。

valueOf()

valueOf方法返回包装对象实例对应的原始类型的值。例如:

new Number(123).valueOf()  // 123
new String("abc").valueOf() // "abc"
new Boolean("true").valueOf() // true

toString()

toString方法返回实例对应的字符串表示。例如:

new Number(123).toString() // "123"
new String("abc").toString() // "abc"
new Boolean("true").toString() // "true"