js根本数据类型: number, string, boolean, null, undefined, symbol

如果咱们想将这些根本数据类型转换成对象类型怎么办呢?

通用办法:Object() 或 new Object()
( 留神: Object() 和 new Object() 行为是统一的也就是一个意思 )

let a = Object( 100 )let b = Object( 'hi man' )let c = Object( true )let d = Object( Symbol( 'only' )  )let e = Object( null ) // 输入为 {}let f = Object( undefined ) // 输入为 {}

特定办法: new + Number | String | Boolean

let a = new Number( 100 )let b = new String( 'hi man' )let c = new Boolean( true )

好了,咱们看到了,这样就转换成了对象,也就是所谓的 包装对象

js包装对象的原型上有两个通用的办法: toString(), valueOf()

let x = new Number( 888 )x.toStirng() // '888'x.valueOf() // 888let y = Object( Symbol() )y.toString() // 'Symbol()'y.valueOf() // Symbol()

留神: 转换为对象后,能够应用对象原型上的所有办法哦

重点来了
上文中的转换都是显示转换,而咱们在理论是应用时是能够 隐式转换

'mei li de yi tian'.length // 字符串隐式转换为字符串对象应用( 666 ).toString() // 数字隐式转换为Number对象应用, 留神: 数字必须加括号能力被隐式转换true.toString() // 布尔值隐式转换成为Boolean对象Symbol( 'only' ).toString() // symbol值被隐式转换为 symbol包装对象

须要留神的是: 隐式转换只是一种长期转换,应用完后会被主动销毁, 并且 转换后的包装对象是只读的不能被批改

let str = 'meili'str.x = 999console.log( str.x ) //undefined