字符串是不可变的。函数
每一个字符是一个16
位的UTF-16
编码单元,这意味着一个Unicode
字符至关于一个或两个JavaScript
字符。编码
即用单引号或双引号括起来的字符序列。spa
'string text' "string text" "中文 español English हिन्दी العربية português বাংলা русский 日本語 ਪੰਜਾਬੀ 한국어"
new String(thing)
由String()
构造函数获得字符串对象prototype
> var s = new String(123) > typeof s 'object' >
区分二者很简单。code
字符串字面量 以及 String()
函数做为普通函数调用时的返回值,这两种状况下获得的是字符串原始值。对象
判断字符串原始值方法为typeof 'xxx'
,获得‘string’
,即ip
> typeof 'ad' 'string' // 字符串原始值
由new String()
构造器函数获得的是字符串对象。字符串
判断字符串对象的方法也为typeof 'xxx'
, 获得‘object’
,即string
> var s = new String(123) > typeof s 'object' // 字符串对象 >
最重要一点,字符串原始值也能够调用字符串对象所具备的方法,由于JavaScript
内部会自动将字符串原始值转化为字符串对象,以调用相关方法,而后恢复。it
function isString (value) { return Object.prototype.toString.call(value) === '[object String]'; }
function isStringPrimitive (value) { return typeof value === 'string'; }
function isString (value) { return Object.prototype.toString.call(value) === '[object String]' && typeof value === 'object'; }