引用类型---基本包装类型

为了便于操作基本类型值,ECMAScript提供了3个特殊的引用类型:Boolean、Number和String。这些类型与其他引用类型相似,但同时也具有与各自的基本类型相应的特殊行为。实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而能够调用一些方法来操作这些数据。

一.基本包装类型概述

    var box = 'abcdefg';                            //定义一个字符串
    var box2 = box.substring(2);                    //截掉字符串前两位
    alert(box2);                                    //cdefg,输出新字符串
变量box是一个字符串类型,而box.substring(2)又说明它是一个对象(PS:只有对象才会调用方法),最后把处理结果赋值给box2。'abcdefg'是一个字符串类型的值,按道理它不应该是对象,不应该会有自己的方法,比如:
alert('abcdefg'.substring(2));                  //cdefg,直接通过值来调用方法

1.字面量写法:

    var box = 'abcdefg';                            //字面量
    box.name = 'hcd';                               //无效属性
    box.age = function () {                         //无效方法
        return 24;
    };
    console.log(box);                               //abcdefg
    console.log(box.substring(2));                  //cdefg
    console.log(typeof box);                        //string
    console.log(box.name);                          //undefined
    console.log(box.age());                         //box.age is not a function

2.new运算符写法:

    var box = new String('abcdefg');                //new运算符
        box.name = 'hcd';                           //有效属性
        box.age = function () {                     //有效方法          
        return 24;
    };
    console.log(box);                               //String {0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", name: "hcd", length: 7, [[PrimitiveValue]]: "abcdefg"}
    console.log(box.substring(2));                  //cdefg
    console.log(typeof box);                        //object
    console.log(box.name);                          //hcd
    console.log(box.age());                         //24
以上字面量声明和new运算符声明很好的展示了他们之间的区别。但有一定还是可以肯定的,那就是不管字面量形式还是new运算符形式,都可以使用它的内置方法。并且Boolean和Number特性与String相同,三种类型可以成为基本包装类型。
PS:在使用new运算符创建以上三种类型的对象时,可以给自己添加属性和方法,但我们建议不要这样使用,因为这样会导致根本分不清到底是基本类型值还是引用类型值。

二.Boolean类型

Boolean类型没有特定的属性或者方法。

三.Number类型

Number类型有一些静态属性(直接通过Number调用的属性,而无须new运算符)和方法。

1.Number静态属性

    属性                  描述
MAX_VALUE               表示最大数
MIN_VALUE               表示最小值
NaN                     非数值
NEGATIVE_INFINITY       负无穷大,溢出返回该值
POSITIVE_INFINITY       无穷大,溢出返回该值
prototype               原型,用于增加新属性和方法

2.Number对象的方法

方  法                        描述
toString()              将数值转化为字符串,并且可以转换进制
toLocaleString()        根据本地数字格式转换为字符串
toFixed()               将数字保留小数点后指定位数并转化为字符串
toExponential()         将数字以指数形式表示,保留小数点后指定位数并转化为字符串
toPrecision()           指数形式或点形式表述数,保留小数点后面指定位数并转化为字符串
    var box = 1000.789;
    console.log(box.toString());                        //'1000.789',转换为字符串,传参可以转换进制
    console.log(box.toLocaleString());                  //1,000.789,本地形式
    console.log(box.toFixed(2));                        //1000.79,小数点保留
    console.log(box.toExponential());                   //1.000789e+3,指数形式,传参会保留小数点
    console.log(box.toPrecision(3));                    //1.00e+3,指数或点形式,传参保留小数点

四.String类型

String类型包含了三个属性和大量的可用内置方法。

1.String对象属性

属  性                    描述
length              返回字符串的字符长度
constructor         返回创建String对象的函数
prototype           通过添加属性和方法扩展字符串定义

String也包含对象的通用方法,比如valueOf()、toLocaleString()和toString()方法,但这些方法都返回字符串的基本值。

2.字符方法

方  法                    描述
charAt(n)               返回指定索引位置的字符
charCodeAt(n)           以编码形式返回指定索引位置的字符
    var box = 'abcdefg';
    console.log(box.charAt(1));                         //b
    console.log(box.charCodeAt(1));                     //98
    console.log(box[1]);                                //b,通过数组方式截取
    PS:box[1]在IE浏览器会显示undefined,所以使用时要慎重。

3.字符串操作方法

方 法                     描述
concat(str1...str2)     将字符串参数串联到调用该方法的字符串
slice(n,m)              返回字符串n到m之间位置的字符串(前闭后开)
substring(n,m)          返回字符串n到m之间位置的字符串(前闭后开)
substr(n,m)             返回字符串n开始的m个字符串
var box = 'abcdefg';
    console.log(box.concat(' is', ' a ', ' string '));          //abcdefg is a  string,原str不会改变 
    console.log(box.slice(3));                                  //defg,从索引3一直结束
    console.log(box.slice(3,5));                                //de,从索引3到索引5之前(也就是索引3,4多对应的的str值)
    console.log(box.substring(3));                              //defg,从索引3一直结束
    console.log(box.substring(3,5));                            //de,从索引3到索引5之前(也就是索引3,4多对应的的str值)
    console.log(box.substr(3));                                 //defg,从索引3一直结束
    console.log(box.substr(3,1));                               //d,从索引3开始切割一个
    console.log(box);                                           //abcdefg,原来的str并不会受到影响

    var box = 'abcdefg';
    console.log(box.slice(-3));                                 //efg,7+(-3)=4位开始
    console.log(box.substring(-3));                             //abcdefg 负数返回全部
    console.log(box.substr(-3));                                //efg,7+(-3)=4位开始

    var box = 'abcdefg';
    console.log(box.slice(3, -1));                      //def 7+(-1)=5, (3,6)
    console.log(box.substring(3, -1));                  //abc 第二参为负,直接转0,
    //并且方法会把较小的数字提前,(0,3)
    console.log(box.substr(3, -1));                     //'' 第二参数为负,直接转0 ,(3,0)

    PS:IE的JavaScript实现在处理向substr()方法传递负值的情况下存在问题,它会返回原始字符串,使用时要切记。

4.字符串位置方法

方 法                            描述
indexOf(str, n)             从n开始搜索的第一个str,并将搜索的索引值返回
lastIndexOf(str, n)         从n开始搜索的最后一个str,并将搜索的索引值返回
    var box = 'abcadeafg';
    console.log(box.indexOf('a'));                      //0
    console.log(box.indexOf('a', 2));                   //3
    console.log(box.lastIndexOf('a'));                  //6
    console.log(box.lastIndexOf('a', 2));               //0,从指定的位置向前搜索
    PS:如果没有找到想要的字符串,则返回-1
示例:找出全部的L
    var box = 'abcadeafg';
    var boxarr = [];                                //存放a位置的数组
    var pos = box.indexOf('a');                     //先获取第一个a的位置
    while (pos > -1) {                              //如果位置大于-1,说明还存在a
        boxarr.push(pos);                           //添加到数组
        pos = box.indexOf('a', pos + 1);            //从新赋值pos目前的位置
    }
    console.log(boxarr);                            //[0, 3, 6]输出

4.大小写转换方法

方  法    描述
toLowerCase(str)        将字符串全部转换为小写
toUpperCase(str)        将字符串全部转换为大写
toLocaleLowerCase(str)  将字符串全部转换为小写,并且本地化
toLocaleupperCase(str)  将字符串全部转换为大写,并且本地化
    var box = 'HCD is a Big Man';                   
    console.log(box.toLowerCase());                 //hcd is a big man,全部小写
    console.log(box.toUpperCase());                 //HCD IS A BIG MAN,全部大写
    console.log(box.toLocaleLowerCase());               //
    console.log(box.toLocaleUpperCase());               //
PS:只有几种语言(如土耳其语)具有地方特有的大小写本地性,一般来说,是否本地化效果都是一致的。

###5.字符串的模式匹配方法
方  法                                    描述
match(pattern)                     返回pattern 中的子串或null
replace(pattern, replacement)      用replacement 替换pattern
search(pattern)                    返回字符串中pattern 开始位置
split(pattern)                     返回字符串按指定pattern 拆分的数组

以上中match()、replace()、serach()、split()在普通字符串中也可以使用。
var box = 'HCD is a Big Man';                   
    console.log(box.match('B'));                        //["B", index: 9, input: "HCD is a Big Man"],找到B,返回B,否则返回null
    console.log(box.search('M'));                       //13,找到M的位置,和indexOf类型相似
    console.log(box.replace('C', 'R'));                 //HRD is a Big Man,把C替换成R
    console.log(box.split(' '));                        //["HCD", "is", "a", "Big", "Man"],以空格分割成字符串
其他方法
方  法    描述
fromCharCode(ascii) 静态方法,输出Ascii码对应值
alert(String.fromCharCode(76));             //L,输出Ascii码对应值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值