内容要点:
1、基本包装类型
2、Boolean类型
3、Number类型
4、String类型
背景:
为了便于操作基本类型的值,ECMAScript提供了3种特殊的引用类型:Booloean、String、Number
这些类型与其他引用类型相似,但却同时具有各自的基本类型相应的特殊行为。
实际上,没读取一个基本类型值,后台就会创建出一个对应的基本包装类型的对象,用调用来操作数据。
1、基本包装类型
var box='Lee';
alert(box); //Lee
alert(typeof box); //string
alert(box.substring(2)); //对象.方法(),明显是引用类型的写法
//虽然它是基本类型,但也是特殊的引用类型,它可以调用系统内置的方法。
(引用类型除了object自身,还包括number、String、RegExp正则表达式)
(1)字面量写法
var box='Lee'; //基本类型;
box.name='Mr.Lee' //给基本类型加属性;
box.age=function(){ //给基本类型加方法;
return 10;
};
alert(box);
alert(typeof box); //string;
alert(box.name); //undefined;
alert(box.age()); //出错;
//基本类型无法给自己创建属性和方法,但可以调用内置属性和方法;
(2)new运算符写法
new为引用类型,可以为自己创建属性和方法
var box=new String('Lee'); //引用类型,String的引用类型
box.name='Ms.Lee';
box.age=function(){
return 10;
};
alert(box);
alert(typeof box); //object;
alert(box.substring(2)); //有效;
alert(box.name); //自定义属性有效;
alert(box.age()); //自定义方法有效;
(注意:若用String创建时,与Object创建对象没有什么区别,一般不建议这样用)
总结:
字面量方法不能自定义属性和方法;
new运算符可以自定义属性和方法。
2、Boolean类型
该类型,没有自定义属性和方法
3、Number类型
具备静态方法和方法,即直接通过number调用的属性,而无需new运算符
案例3-1:静态属性
var box=100;
alert(box.MAX_VALUE); //写法叫属性;
alert(Number.MAX_VALUE); //这种写法叫做静态属性;
(1)静态属性
MAX_VALUE——最大值
MIN_VALUE——最小值
NaN——非数值
prototype——原型,用于添加新属性和方法。
(2)对象的方法
toString()——将数值转换为字符串
toLocaleString()——根据本地数字格式转化为字符串
toFixed()——将数字保存到小数点后指定位数并转化为字符串
toExponential()——将数字以指数表示,保存小数点到指定位数并转化为字符串
toPrecision()——指数形式或点表示,保留小数点到是定位位数并转换为字符串
案例3-2:对象的方法
var box=1000.234;
alert(box.toFiexd(2)); //表示小数点保留两位,并进行四舍五入;
alert(box.toExponential()); //表示以指数形式,并转化字符串,类似于该值除以1000的值,以“e”连接表示
alert(box.toPrecision); //10以上以指数表示,小于10以小数点表示。
4、String类型
有3种属性和大量内置方法
(1)对象属性:length——返回字符串长度
constructor——返回创建String对象的方法
prototype——通过添加属性和方法扩展字符串定义
(2)对象通用方法:valueOf()、toLocaleString()、toString(),这些方法均返回字符串的基本值
(3)字符方法:charAt(n)——返回索引的(从0开始)
charCodeAt(n)——返回以索引的从0开始的unicode编码。
(4)字符串操作方法:concat(str1...str2)——表示参数字符串的连接,即将字符串参数串联到调用该方法的字符串
slice(n,m)——表示从n到m的字符串
substring(n,m)——同上
substr(n,m)——返回字符串从n开始,m个字符串
(5)字符串位置方法:indexof()——返回从初始位置到字符串参数第一次出现的位置
lastIndexOf()——返回从末尾位置到字符串参数第一次出现的位置
案例4-1:
var box='Mr.Lee';
alert(box.length); //7
案例2:字符串方法
alert(box.charAt(1)); //r
alert(box.charCodeAt(4)); //返回L的ASCII的编码
案例3:字符串操作方法
3-1:正数
var box='Mr.Lee';
alert(box.slice(4,6)); //Lee
alert(box.substring); //Lee
alert(box.substr(4,6)); //从第四个开始,选6个
3-2:有1个负数
var box='Mr.Lee';
alert(box.slice(-2)); //7+(-2)=5,从第五位开始取,即ee
alert(box.substring(-2)); //负数,即返回全部的字符串;
alert(box.substr(-2)); //与slice()方式相同;
3-3:有2个参数
alert(box.slice(2,-1)) ; //7+(-1)=6 ,即从2到6的位数
alert(box.slice(-2,-1)); //7+(-1)=6,7+(-2)=5,(5,6),e
alert(box.substring(2,-1)); //参数如果是负数,直接定为0,若第二个参数比第一个小,则互换位置,从(2,0)到(0,2)
alert(box.substr(2,-1)); //第二个参数为负数,直接为0,(2,0),返回空
案例4:字符串位置方法
var box='Mr.Lee is Lee';
alert(box.indexOf('L')); //4,
alert(box.lastIndexOf('L')); //11;
alert(box.indexOf('L',5)); //11,返回从第五个位置向后搜索开始到L第一次出现的位置
alert(box.lastIndexOf('L')); //4,返回从第五个位置开始向前搜索到L第一次出现的位置
扩展——搜索所有L
var box='Mr.Lee is Lee, hello Lan';
var boxarr=[];
var pos=box.indexOf('L');
while(pos>-1){
boxarr.push(pos);
pos=box.indexOf('L',pos+1);
}
alert(boxarr); //4,11,21
(6)转换大小写
toLowerCase()
toUpperCase()——转换为大小写
toLocaleLowerCase()
toLocaleUpperCase()——本地特有的大小写
(7)字符串正则表达式方法
match()——返回查找的字符或者null
replace(n,m)——表示用m替换n
search()——查询所在的位置
split()——表示某一个符号或字符分割数组
案例7
var box='Mr.Lee';
alert(box.match('L')); //找到L返回L
alert(box.search('L')); //找到L的位置;
alert(box.replace('L','Q')); //将Q代替L
alert(box.splice(' ')); //分割为数组,若以空格分割,即为“Mr”和“Lee”
(8)其他方法
localeCopare()——字符串比较,返回0,1,-1
案例8-1:比较字符串大小
var box='Lee';
alert(box.localeCompare('Lee')); //0
alert(box.localeCompare('Aee')); //1,A在L之前,返回1
alert(box.localeCompare('Zee')); //-1,Z在L之后,返回-1