基本包装类型

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

基本包装类型在看基础包装类型之前,回顾⼀下什么是引⽤类型:
引⽤类型是⼀种数据结构,⽤于将数据和功能组织在⼀起。

它包括:
Object类型
Array类型
Date类型
RegExp
Function类型
什么是基本包装类型?
基本包装类型是ECMAScript为了便于操作基本类型值提供的三种特殊的引⽤类型:
Boolean
Number
String
引⽤类型和基本包装类型的区别在于对象的⽣存期:
引⽤类型
使⽤new操作符创建的应⽤类型实例,在执⾏流离开当前作⽤域之前都⼀直保存在内存当中。

基本包装类型
⾃动创建的基本包装类型对象,只存在于⼀⾏代码执⾏的瞬间,然后⽴即被销毁。

Number类型
Number是与数字值对应的引⽤类型。

在Number类型中,valueOf()⽅法返回对象表⽰的基本类型的数值;toLocaleString()和toString()⽅法返回字符串形式的数值。

Number类型⽅法:
① toString()
传递⼀个表⽰基数的参数,告诉它返回⼏进制数值的字符串形式。

var num = 10;
console.log('不传参数:' + num.toString()); //不传参数:10
console.log('⼆进制:' + num.toString(2)); //⼆进制:1010
console.log('⼋进制:' + num.toString(8)); //⼋进制:12
console.log('⼗进制:' + num.toString(10)); //⼗进制:10
console.log('⼗六进制:' + num.toString(16)); //⼗六进制:a
② toFixed()
作⽤:该⽅法会按照指定的⼩数位返回数值的字符串表⽰,并有⾃动舍⼊的特性。

该⽅法很适合处理货币值
var num = 10;
console.log('显⽰两位⼩数: ' + num.toFixed(2));
var num1 = 11.12354;
console.log('显⽰三位⼩数: ' + num1.toFixed(3));
/* 输出
显⽰两位⼩数: 10.00
显⽰三位⼩数: 11.124
*/
③ toExponential()
作⽤:格式化数值,该⽅法会返回指数表⽰法(也称e表⽰法)
该⽅法接收⼀个参数,表⽰显⽰⼏位⼩数
let num = 1000;
console.log('指数表⽰:' + num.toExponential(2));
/* 输出
let num = 1000;
console.log('指数表⽰:' + num.toExponential(2));
*/
③ toPrecision()
toPrecision()接收⼀个参数,表⽰数值的所有数字的位数,然后则根据要显⽰的位数,看哪种格式适合表达则返回哪种格式。

let num1 = 1200;
console.log('⼀位数显⽰:' + num1.toPrecision(1));
console.log('两位数显⽰:' + num1.toPrecision(2));
console.log('四位数显⽰:' + num1.toPrecision(4));
console.log('六位数显⽰:' + num1.toPrecision(6));
/* 输出
⼀位数显⽰:1e+3
两位数显⽰:1.2e+3
四位数显⽰:1200
六位数显⽰:1200.00
*/
String类型
string类型是字符串的对象包装类型。

String对象的⽅法也可以在所有基本的字符串值中访问到。

其中,valueOf()、toLocaleString()、toString()⽅法,都返回对象所表⽰的基本字符串值。

String类型的每个实⼒都有⼀个length属性,表⽰字符串包含多个字符。

let strValue = 'Give me a book';
console.log('strValue的length:' + strValue.length);
/* 输出
strValue的length:14
*/
String类型提供多种⽅法:
字符⽅法
① charAt()
作⽤:⽤于访问字符串中特定的字符。

该⽅法接收⼀个参数,即要访问的字符的索引。

然后返回该索引位置的字符。

var stringValue = 'QuanzhiGaoshou_yexiu';
console.log(stringValue.charAt(5));
/* 输出
h
*/
② charCodeAt()
作⽤:⽤于访问字符串中特定的字符。

该⽅法接收⼀个参数,即要访问的字符的索引。

然后返回该索引位置的字符的字符编码。

var stringValue = 'QuanzhiGaoshou_yexiu';
console.log(stringValue.charAt(5)); //输出 h
console.log(stringValue.charCodeAt(5)); //输出 104
另外,访问字符还可以⽤⽅括号⽅法:
var stringValue = 'QuanzhiGaoshou_yexiu';
console.log(stringValue[5]); //输出 h
③ concat()
作⽤:拼接字符串,返回⼀组新字符串
var stringValue = 'QuanzhiGaoshou ';
console.log('New string: ' + stringValue.concat("Yexiu"));
console.log('some String: ' + stringValue.concat("Yexiu","!","~"));
console.log('stringValue: ' + stringValue);
/* 输出
New string: QuanzhiGaoshou Yexiu
some String: QuanzhiGaoshou Yexiu!~
stringValue: QuanzhiGaoshou
*/
注:拼接字符串最常⽤的还是“+”连字法
④ slice() ⑤ substr() ⑥ substring()
三个⽅法都是基于字符串创建新字符串,且都会返回被操作字符串的⼀个⼦字符串。

三个⽅法都接收2个参数,第⼆个可选:
第⼀个参数:开始的位置(索引)
第⼆个参数:slice()和substring()两个⽅法的第⼆参数都是结束的位置,substr()是需要返回字符的个数。

//例1
var stringValue = 'QuanzhiGaoshou yexiu';
console.log('slice1:' + stringValue.slice(5));
console.log('substr1:' + stringValue.substr(5));
console.log('substring1: ' + stringValue.substring(5));
/*
slice:hiGaoshou yexiu
substr:hiGaoshou yexiu
substring: hiGaoshou yexiu
*/
从例1可以看出,三个⽅法在只接收⼀个参数的情况下,输出结果都是相同的,它们在没接收到第⼆个参数的时候,都默认结束位置为字符末尾。

//例2
var stringValue = 'QuanzhiGaoshou yexiu';
console.log('slice2:' + stringValue.slice(5,6));
console.log('substr2:' + stringValue.substr(5,6));
console.log('substring2: ' + stringValue.substring(5,6));
/* 输出
slice2:h
substr2:hiGaos
substring2: h
*/
可以看出,slice 和 substring 的第⼆参数都是指定结束的位置。

⽽substr的第⼆参数是指定输出字符的个数。

//例3
var stringValue = 'QuanzhiGaoshou yexiu';
console.log('slice3:' + stringValue.slice(5,3));
console.log('substr3:' + stringValue.substr(5,3));
console.log('substring3: ' + stringValue.substring(5,3));
/* 输出
slice3:
substr3:hiG
substring3: nz
*/
从上例(例3)可以看出, 如果第⼆个参数⼩于起始位置(也就是第⼀参数),substr()返回的是字符的个数,所以没受到影响。

区别在于slice()和substring()⽅法,两个的第⼆参数都是结束位置,但是slice()⽅法在结束位置⼩于起始位置的时候,会返回空。

substring()则会返回这两个索引之间的字符。

这三个⽅法还有个主要的区别是在参数传⼊负数的情况下:
//例4
var stringValue = 'QuanzhiGaoshou yexiu';
console.log('slice4:' + stringValue.slice(-4));
console.log('substr4:' + stringValue.substr(-4));
console.log('substring4: ' + stringValue.substring(-4));
console.log('slice5:' + stringValue.slice(5,-4));
console.log('substr5:' + stringValue.substr(5,-4));
console.log('substring5: ' + stringValue.substring(5,-4));
/* 输出
slice4:exiu
substr4:exiu
substring4: QuanzhiGaoshou yexiu
slice5:hiGaoshou y
substr5:
substring5: Quanz
*/
slice()⽅法在处理负数时的具体表现就是,将从被操作字符的开始往前数。

按照书中的说法就是:
slice()⽅法将会将传⼊的负值与字符串的长度相加。

substr()如果是第⼀个参数接收负数,处理⽅法和slice()⼀样。

⽽如果第⼆个参数传⼊负数,则会转为0;
substring()⽅法则是将所有负数转为0;
⑦ indexOf() ⑧lastIndexOf()
两个⽅法是字符串的位置⽅法,从⼀个字符串中搜索给定的⼦字符串然后返回字符串的位置(如果没找到该字符串,则返回-1)。

两个⽅法的区别是indexOf()从字符串开头向后搜索,lastIndexOf()则是末尾向前搜索。

var stringValue = 'Come on~ today!';
console.log('没找到: ' + stringValue.indexOf('yes'));
console.log('indexOf: ' + stringValue.indexOf('o'));
console.log('lastIndexOf: ' + stIndexOf('o'));
/* 输出
没找到: -1
indexOf: 1
lastIndexOf: 10
*/
两个⽅法都接受第⼆参数,第⼆参数主要是指定从什么位置开始搜索,indexOf()会忽略指定位置之前的字符往后搜索;lastIndexOf()则刚好相反,会忽略指定位置之后的字符往前搜索。

var stringValue = 'Come on~ today!';
console.log('indexOf: ' + stringValue.indexOf('o',3));
console.log('lastIndexOf: ' + stIndexOf('o',3));
/* 输出
indexOf: 5
lastIndexOf: 1
*/
应⽤第⼆参数,可遍历⼀个长字符串,找出查找字符的所有位置:
var stringValue = `hello, I've been watching TV recently. I also have a cute cat.`;
var positions = [];
var pos = stringValue.indexOf('a');
while(pos > -1){
positions.push(pos);
pos = stringValue.indexOf('a', pos + 1);
}
console.log('字符串内所有字符a的下标是: ' + positions);
/* 输出
字符串内所有字符a的下标是: 18,41,47,51,59
*/
⑨ trim()
该⽅法会创建⼀个字符串副本,删除该字符串前后的空格(中间的空格不受影响)。

var stringValue = ' Hei~ happy! ';
console.log(stringValue);
console.log(stringValue.trim());
/* 输出
Hei~ happy!
Hei~ happy!
*/
注:另有trimLeft()和trimRight()⽅法分别删除左右空格
⑩ toLowerCase() toLocaleLowerCase() toUpperCase() toLocaleUpperCase()
这四种⽅法的主要作⽤是转换⼤⼩写:
var stringValue = ' Hei~ happy! ';
console.log('toLowerCase: ' + stringValue.toLowerCase());
console.log('toLocaleLowerCase: ' + stringValue.toLocaleLowerCase());
console.log('toUpperCase: ' + stringValue.toUpperCase());
console.log('toLocaleUpperCase: ' + stringValue.toLocaleUpperCase());
/* 输出
toLowerCase: hei~ happy!
toLocaleLowerCase: hei~ happy!
toUpperCase: HEI~ HAPPY!
toLocaleUpperCase: HEI~ HAPPY!
*/
这其中,toLowerCase()和toLocaleLowerCase()以及toUpperCase()和toLocaleUpperCase()的区别主要是时区。

match()
字符串的模式匹配⽅法,只接收⼀个参数,返回⼀个数组。

var text = 'cat, bat, sat, fat, ymt';
var pattern = /.at/;
var matches = text.match(pattern);
console.log(matches);
/* 返回⼀个数组
[ 'cat',
index: 0,
input: 'cat, bat, sat, fat, ymt',
groups: undefined ]
*/
返回的第⼀项是匹配的项,第⼆项是位置索引。

之所以上例只匹配了⼀项,是因为匹配规则中不是全局模式。

var text = 'cat, bat, sat, fat, ymt';
var pattern = /.at/;
var pattern1 = /.at/g;
var matches = text.match(pattern);
var matches1 = text.match(pattern1);
console.log('matches: ' + matches);
console.log('matches1: ' + matches1);
/* 输出
matches: cat
matches1: cat,bat,sat,fat
*/
search()
该⽅法接收⼀个参数,返回字符串中第⼀个匹配项的索引;
如果没有找到匹配项,则返回-1。

var text = 'ymt, cat, bat, sat, fat';
var pos = text.search(/at/);
console.log('at 第⼀次出现的位置: ' + pos);
/* 输出
at 第⼀次出现的位置: 6
*/
replace()
该⽅法接收两个参数,第⼀个参数可以是RegExg对象或者⼀个字符串,第⼆个参数是⼀个字符串或者⼀个参数。

var text = 'ymt, cat, bat, sat, fat';
var result = text.replace(/at/g, 'ond'); //全局更改
var result1 = text.replace(/at/, 'ond'); //只更改第⼀个匹配项1
var result2 = text.replace('at', 'ond'); //只更改第⼀个匹配项1
console.log('result: ' + result);
console.log('result1: ' + result1);
console.log('result2: ' + result2);
/* 输出
result: ymt, cond, bond, sond, fond
result1: ymt, cond, bat, sat, fat
result2: ymt, cond, bat, sat, fat
*/
如上例,如果第⼀个参数是字符串或者是RegExg对象但没全局模式,那么只会更改第⼀个匹配项;
如果需要所有匹配项都更改,则需要接受RegExg对象并设置全局。

replace()第⼆参数可以实现更精细的提替换操作:
⽐如⽤户输⼊的值,会输⼊⼀些转义字符,但这些转义字符在html中有作⽤,不⼀定会显⽰出来,这就可以⽤到。

var text = '2*3 > 2+3';
function htmlEscape(text){
return text.replace(/[<>]/g,function(match,pos,originalText){
switch(match){
case '<':
return '&lt;';
case '>':
return '&gt;';
}
})
}
console.log(htmlEscape(text));
/* 输出
2*3 &gt; 2+3
*/
split()
split()⽅法容易和slice()⽅法混淆,但它们实际上作⽤不同:
slice()是返回被操作字符串的⼀段指定的⼦字符串。

split()是分割字符串,指定⼀个分割符将⼀个字符串分割成多个⼦字符串,并将结果放⼊⼀个数组中。

splice()⽤于数组的操作⽅法
split()⽅法第⼆参数可选,⽤于指定数组长度。

var text = 'ymt, cat, bat, sat, fat';
console.log(text.split(','));
console.log(text.split(',',3));
/* 输出
[ 'ymt', ' cat', ' bat', ' sat', ' fat' ]
[ 'ymt', ' cat', ' bat' ]
*/。

相关文档
最新文档