Number 对象是原始数值的包装对象。
语法:
var num = new Number(value);
注: 如果一个参数值不能转换为一个数字将返回 NaN (非数字值)。
对象属性
序号 | 属性 | 描述 |
---|---|---|
1 | MAX_VALUE | 可表示的最大的数,MAX_VALUE 属性值接近于 1.79E+308。大于 MAX_VALUE 的值代表 "Infinity"。 |
2 | MIN_VALUE | 可表示的最小的数,即最接近 0 的正数 (实际上不会变成 0)。最大的负数是 -MIN_VALUE,MIN_VALUE 的值约为 5e-324。小于 MIN_VALUE ("underflow values") 的值将会转换为 0。 |
3 | NaN | 非数字值(Not-A-Number)。 |
4 | NEGATIVE_INFINITY | 负无穷大,溢出时返回该值。该值小于 MIN_VALUE。 |
5 | POSITIVE_INFINITY | 正无穷大,溢出时返回该值。该值大于 MAX_VALUE。 |
6 | prototype | Number 对象的静态属性。使您有能力向对象添加属性和方法。 |
7 | constructor | 返回对创建此对象的 Number 函数的引用。 |
例:
console.log("TypeScript Number 属性: ");
console.log("最大值为: " + Number.MAX_VALUE);
console.log("最小值为: " + Number.MIN_VALUE);
console.log("负无穷大: " + Number.NEGATIVE_INFINITY);
console.log("正无穷大:" + Number.POSITIVE_INFINITY);
console.log("非数字值是:"+ Number.NaN)
// TypeScript Number 属性:
// 最大值为: 1.7976931348623157e+308
// 最小值为: 5e-324
// 负无穷大: -Infinity
// 正无穷大:Infinity
// 非数字值是:NaN
prototype 实例
function employee(id:number,name:string) {
this.id = id
this.name = name
}
var emp = new employee(123,"admin")
employee.prototype.email = "admin@runoob.com"
console.log("员工号: "+emp.id)
console.log("员工姓名: "+emp.name)
console.log("员工邮箱: "+emp.email)
// 员工号: 123
// 员工姓名: admin
// 员工邮箱: admin@runoob.com
Number 对象方法
Number对象支持以下方法:
序号 | 方法 | 描述 | 实例 |
---|---|---|---|
1 | toExponential() | 把对象的值转换为指数计数法。 | var num1 = 1225.30 |
2 | toFixed() | 把数字转换为字符串,并对小数点指定位数。 | var num3 = 177.234 // 177 // 177.23 // 177.234000 |
3 | toLocaleString() | 把数字转换为字符串,使用本地数字格式顺序。 | var num = new Number(177.1234); // 输出字符串类型的177.1234 |
4 | toPrecision() [注] | 把数字格式化为指定的长度。 | var num = new Number(7.123456); // 7.123456 // 7 // 7.1 |
5 | toString() | 把数字转换为字符串,使用指定的基数。数字的基数是 2 ~ 36 之间的整数。若省略该参数,则使用基数 10。 | var num = new Number(10); // 输出10进制:10 // 输出2进制:1010 // 输出8进制:12 |
6 | valueOf() | 返回一个 Number 对象的原始数字值。 | var num = new Number(10); // 输出:10 |
注:
toPrecision()
numberObj.toPrecision( precision )
- 没有提供precision参数或其值为undefined时,会转为使用toString()的方法处理;例如:7.123456 .toPrecision(); 输出:'7.123456';
- 数字的有效位数大于precision,将会根据第precision + 1位的有效数字进行四舍五入;例如:153624.2354.toPrecision(8);输出:'153624.24';
- 数字的有效位数大于precision且数字的整数部分位数大于precision,将会根据第precision + 1位的有效数字进行四舍五入后,用指数计数法表示;例如:153624.23.toPrecision(3);输出:'1.54e+5';(e+5为10的5次方)
- 数字的有效位数小于precision,将用定点表示法在小数部分填充相应位数的0;例如:153624.23.toPrecision(10);输出:'153624.2400';
String 对象用于处理文本(字符串)。
语法
var txt = new String("string");
或者更简单方式:
var txt = "string";
String 对象支持的属性:
属性 | 描述 |
---|---|
constructor | 对创建该对象的函数的引用。 |
length | 返回字符串的长度。 |
prototype | 允许您向对象添加属性和方法。 |
constructor、prototype的使用方法同上。
var uname = new String("Hello World")
console.log("Length "+uname.length) // 11
String方法
方法 | 描述 | 实例 |
---|---|---|
charAt() | 返回在指定位置的字符。 | var str = new String("RUNOOB"); console.log("str.charAt(0) 为:" + str.charAt(0)); // R console.log("str.charAt(1) 为:" + str.charAt(1)); // U console.log("str.charAt(2) 为:" + str.charAt(2)); // N console.log("str.charAt(3) 为:" + str.charAt(3)); // O console.log("str.charAt(4) 为:" + str.charAt(4)); // O console.log("str.charAt(5) 为:" + str.charAt(5)); // B |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 | var str = new String("RUNOOB"); console.log("str.charCodeAt(0) 为:" + str.charCodeAt(0)); // 82 console.log("str.charCodeAt(1) 为:" + str.charCodeAt(1)); // 85 console.log("str.charCodeAt(2) 为:" + str.charCodeAt(2)); // 78 console.log("str.charCodeAt(3) 为:" + str.charCodeAt(3)); // 79 console.log("str.charCodeAt(4) 为:" + str.charCodeAt(4)); // 79 console.log("str.charCodeAt(5) 为:" + str.charCodeAt(5)); // 66 |
concat() | 连接两个或更多字符串,并返回新的字符串。 | var str1 = new String( "RUNOOB" ); var str2 = new String( "GOOGLE" ); var str3 = str1.concat( str2 ); console.log("str1 + str2 : "+str3) // RUNOOBGOOGLE |
indexOf() | 返回某个指定的字符串值在字符串中首次出现的位置。 | var str1 = new String( "RUNOOB" ); var index = str1.indexOf( "OO" ); console.log("查找的字符串位置 :" + index ); // 3 |
lastIndexOf() | 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。 | var str1 = new String( "This is string one and again string" ); var index = str1.lastIndexOf( "string" ); console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 29 index = str1.lastIndexOf( "one" ); console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 15 |
localeCompare() [注] | 用本地特定的顺序来比较两个字符串。 | var str1 = new String( "This is beautiful string" ); var index = str1.localeCompare( "This is beautiful string"); console.log("localeCompare first :" + index ); // 0 |
match() | 查找找到一个或多个正则表达式的匹配。 | var str="The rain in SPAIN stays mainly in the plain"; // 如果找不到的返回null |
replace() | 替换与正则表达式匹配的子串 | var re = /(\w+)\s(\w+)/; var str = "zara ali"; var newstr = str.replace(re, "$2, $1"); console.log(newstr); // 'ali, zara' |
search() | 检索与正则表达式相匹配的值 | var re = /apples/gi; // Contains Apples // 找到匹配的返回0,找不到返回-1 |
slice() [注] | 提取字符串的片断,并在新的字符串中返回被提取的部分。 | var str = "Apples are round, and apples are juicy."; var str2 = str.slice(0,16); // 'Apples are round' |
split() | 把字符串分割为子字符串数组。 | var str = "Apples are round, and apples are juicy."; // [ 'Apples', 'are', 'round,' ] // 第二个参数为分割份数,如果为负数则无效,会返回全量的分割 |
substr() | 从起始索引号提取字符串中指定数目的字符。 | var str = "Apples are round, and apples are juicy."; // 'Apples' |
substring() | 提取字符串中两个指定的索引号之间的字符。 (不包括end下标的字符) | var str = "RUNOOB GOOGLE TAOBAO FACEBOOK"; // 'RUNOOB GOO' // 'B GOOGLE TAOBAO FACEBOOK' |
toLocaleLowerCase() | 根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。 | var str = "Runoob Google"; // 'runoob google' |
toLocaleUpperCase() | 据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。 | var str = "Runoob Google"; // 'RUNOOB GOOGLE' |
toLowerCase() | 把字符串转换为小写。 | var str = "Runoob Google"; // 'runoob google' |
toString() | 返回字符串。 | var str = "Runoob"; console.log(str.toString( )); // 'Runoob' |
toUpperCase() | 把字符串转换为大写。 | var str = "Runoob Google"; // 'RUNOOB GOOGLE' |
valueOf() | 返回指定字符串对象的原始值。 | var str = new String("Runoob"); console.log(str.valueOf( )); // 'Runoob' |
注:
A.localeCompare( B )
- 当A与B相等时,返回 0;
- 当A包含B时,返回 1;
- 当A不包含B时,返回 -1;
string.slice(start,end)
- string.slice(start,end) 提取字符串指定位置的字符(包括下标为end的字符)
- string.slice(start) 从下标start开始,到字符串结束,提取字符串;如果start为负数则从结束位置开始,倒着提取例如:"Apple sare round".slice(-5),输出:'round'