第十章 String()

来源:互联网 发布:襄阳seo 编辑:程序博客网 时间:2024/06/06 09:04

10.1 String()对象概要

String()构造函数用于创建字符串对象和字符串原始值。

//创建string对象var stringObject = new String('foo');console.log(stringObject);//foo(o='f',1='o',2='o')console.log(typeof stringObject);//'object'//创建string字面量/原始值var stringObjectWithoutNewKeyword = String('foo');console.log(stringObjectWithoutKeyword);//'foo'console.log(typeof stringObjectWithoutKeyword);//stringvar stringLiteral = 'foo';console.log(stringLiteral);//'foo'console.log(typeof stringLiteral);

10.2 String()参数

String()走高函数接收一个参数,创建的字符串值。

var stringObject = new String('foo');console.log(stringObject);//foo(o='f',1='o',2='o')

!当来自String()构造函数的实例和new关键字一起使用的时,会生成一个实际的复杂对象。由于会出现与typeof操作符相关的潜在问题。我们应该闭免这样做(而使用字面量/原始数字)。typeof操作符会将复杂的字符串对象报告为“对象”,而不是我们所认为的原始标签(‘string’).此外,字面量/原始值的编写更加迅速,也更为简洁。

10.3 String()属性和方法

属性
prototype
方法
fromCharCode()

10.4 字符串对象实例属性和方法

实例属性
constructor
length
实例方法
charAt()
charCodeAt()
concat()
indexOf()
lastIndexOf()
localeCompare()
match()
quote()
replace()
search()
slice()
split()
substr()
toLocalLowerCase()
toLocalUpperCase()
toLowerCase()
toString()
toUpperCase()
valueOf()

0 0
原创粉丝点击