TypeScript String(字符串)
String 对象用于处理文本(字符串)。
语法
var txt = new String("string");
或者更简单方式:
var txt = "string";
String 对象属性
下表列出了 String 对象支持的属性:
| 序号 | 属性 & 描述 | 实例 |
|---|---|---|
| 1. | constructor
对创建该对象的函数的引用。 |
var str = new String( "This is string" );
console.log("str.constructor is:" + str.constructor)
输出结果: str.constructor is:function String() { [native code] }
|
| 2. | length
返回字符串的长度。 |
var uname = new String("Hello World")
console.log("Length "+uname.length) // 输出 11 |
| 3. | prototype
允许您向对象添加属性和方法。 |
function employee(id:number,name:string) {
this.id = id
this.name = name
}
var emp = new employee(123,"admin")
employee.prototype.email="admin@NowJava.com" // 添加属性 email
console.log("员工号: "+emp.id)
console.log("员工姓名: "+emp.name)
console.log("员工邮箱: "+emp.email)
|