javascript---基本包装对象

总结:

  • 基本包装类型其实总结起来就是,Number和String类型!!!  以下是本人觉得是十分常用的属性和方法。

 

 


1.Number的属性和方法

 属性:

//静态属性无需new
    1.MIN_VALUE                //最小值
    2.NaN                    //非数值
    3.NEGATIVE_INFINITY        //负无穷大,溢出返回
    4.POSITIVE_INFITITY        //无穷大,溢出返回
    5.prototype                //原型,用于增加新属性和方法
    6.MAX_VALUE                //最大值
    如:
    a1ert(Number.MAX_VALUE);
    alert(Number.NaN);

方法:

toString()

toLocaleString()

toFixed()

toExponential()

toPrecision()

    var box=1000.75284;
    alert(typeof box);
    alert(typeof box.toString())        //把数值转换成字符串
    alert(box.toLocaleString())            //本地化
    alert(box.toFixed(2))                //小数点保留两位,并转化字符串
    alert(box.toExponential())            //以指数形式,并转化成字符串
    alert(box.toPrecision(3))            //以指数形式,传参是保留小数点后面指定位数并转化为字符串        1.00E+3

 


 

2.String的属性和方法

属性:

    //普通属性
    var box='String    is String';
    alert(box.length);                //字符串长度
    alert(box.constructor)            //返回创建String对象的函数
    alert(box.prototype)            //通过添加属性和方法扩张字符串定义

方法:

1.字符方法:

charAt()

charCodeAt()

2.字符串方法:

concat()

slice()

substring()

substr()

    //字符方法
    alert(box.charAt(1));            //返回指定下标值,索引从0开始        t
    alert(box.charCodeAt(1));        //返回的是指定下标值得ASCALL码         116
    //字符串操作方法
    alert(box.concat('is ','string ','!!'));            //将字符串参数串联到调用方法的字符串
    alert(box.slice(2,5));                            //返回从2到5的字符串                rin
    alert(box.substring(2,5));                                    //同上 
    alert(box.substr(2,2));            //从第二个开始选2个        ri

注意:上面要分清是字符方法和字符串方法,是不一样的。

3.字符串位置方法

indexOf()

lastIndexOf()

    //字符串位置方法
    alert(box.indexOf('r'))                    //返回从初始位置搜素r的第一次出现的位置            2        找不到返回-1
    alert(box.lastIndexOf('r'))                //返回从末尾位置搜索r的第一次出现的位置            12
    alert(box.indexOf('r',5))                //从第五个位置开始搜索                12
    alert(box.lastIndexOf('r',5))            //从第五个位置开始向前搜索            2
    alert(box.toLowerCase())                //转换成小写        本地化toLocaleLowerCase()
    alert(box.toUpperCase())                //转换成大写        同上

4.字符串模式匹配方法

match()

search()

replace()

split()

    //字符串模式匹配方法
    alert(box.match('r'))            //返回查找到的字符串,找不到返回null
    alert(box.search('r'))            //返回位置,找不到返回-1
    alert(box.replace('r','q'))        //替换
    alert(box);
    alert(box.split(' '))            //以空格为标准分隔成数组
    alert(box)

注意:字符串匹配方法后面出现了alert(box)是为了验证是否改变了原来的对象数组,验证后发现,并没有改变。

 

posted @ 2016-03-18 16:42  GacentJohn  阅读(121)  评论(0编辑  收藏  举报