码迷,mamicode.com
首页 > Web开发 > 详细

JS基本包装类型

时间:2017-04-21 19:01:27      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:操作方法   下标   引用类型   new   替换   arc   box   数字格式化   define   

一.基本包装类型概述
//是基本类型,但是又是特殊的引用类型。
var box = ‘Mr.Lee‘; //基本类型
alert(box.substring(2)); //对象.方法(参数),这种写法明显是引用类型的写法
//substring索引从0开始,从第2个位置开始截取到末尾的字符串
alert(‘Mr.Lee‘.substring(2)); //这样的写法也是可行的。

字面量写法
var box = ‘Mr.Lee‘; //基本类型
box.name = ‘Lee‘; //给基本类型添加属性
box.age = function()//给基本类型添加方法
{
return 100;
};
alert(box);
alert(typeof box);
alert(box.name); //undefined
alert(box.age()); //出错
//基本类型是无法给自己创建属性和方法的。

new运算符写法
var box = new String(‘Mr.Lee‘); //String的引用类型
box.name = ‘Lee‘;
box.age = function()
{
return 100;
};
alert(box);
alert(typeof box); //Object
alert(box.name);
alert(box.age());

var box = 100;
alert(box.MAX_VALUE); //这种写法叫做属性
alert(Number.MAX_VALUE); //这种写法(类型.属性),叫做静态属性

var box = 1000.789;
//alert(typeof box.toString()); //将数值转化为字符串,并且可以转换进制
//alert(box.toLocaleString()); //根据本地数字格式化转换为字符串
//alert(box.toFixed(2)) //小数点保留两位,并转换字符串。
//alert(box.toExponential()); //以指数形式,并转换字符串。
//alert(box.toPrecision(8)); //根据传参来决定指数或者点数

四.String类型
//String类型包含了三个属性和大量的可用内置方法
var box = ‘Mr.Lee‘;
//alert(box.length); //返回字符串的字符长度
//alert(box.constructor);//返回创建String对象的函数
//alert(box.prototype); //通过添加属性和方法扩展字符串定义

字符方法
var box = ‘Mr. Lee‘;
//alert(box.charAt(1)); //返回指定下标的值
//alert(box.charCodeAt(4));//返回的是acssii码

字符串操作方法
var box = ‘Mr. Lee‘;
//alert(box.concat(‘ is‘,‘ Teacher‘,‘ !‘)); //字符串连接
//alert(box.slice(4,6)); //返回字符串n到m之间位置的字符串
//alert(box.substring(4,6)); //返回字符串n到m之间位置的字符串
//alert(box.substr(4,6)); //返回字符串n开始的第m个字符串
//alert(box.slice(-2)); //7+(-2)=5,第5位开始,ee。
//alert(box.substring(-2)); //负数返回全部字符串
//alert(box.substr(-2)); //7+(-2)=5,第5位开始,ee。
//alert(box.slice(2,-1)); //7+(-1)=6,(2,6);
//alert(box.slice(-2,-1)); //7+(-2)=5, 7+(-1)=6, (5,6)
//alert(box.substring(2,-1)); //参数如果是负数,直接返回0,(2,0)如果第二个参数比第一个小,那么变成(0,2);
//alert(box.substr(2,-1)); //第二个参数为负数,直接返回0。(2.0)从第二个开始选择0个,也就是空值。

字符串位置方法
var box = ‘Mr. Lee is Lee‘;
//alert(box.indexOf(‘L‘)); //返回从初始位置搜索L第一次出现的位置,4。
//alert(box.lastIndexOf(‘L‘)); //返回从末尾位置搜索L第一次出现的位置,11。
//alert(box.indexOf(‘L‘,5)); //从第5个位置开始搜索L第一次出现的位置,11。
//alert(box.lastIndexOf(‘L‘,5)); //从第5个位置开始向前搜索L第一次出现的位置,4。
//alert(box.indexOf(‘,‘)); //找不到则返回-1
//示例:找出全部的L
var boxarr = []; //存放L位置的数组
var pos = box.indexOf(‘L‘); //先获得第一个L的位置,放进变量pos
while(pos > -1)
{
boxarr.push(pos); //添加到数组
pos = box.indexOf(‘L‘,pos+1); //重新赋值pos目前的位置
}
alert(boxarr);

大小写转换方法
var box = ‘Mr. Lee‘;
//alert(box.toLowerCase()); //全部转换为小写
//alert(box.toUpperCase()); //全部转换为大写
//alert(box.toLocaleLowerCase()); //全部转换为小写且本地化
//alert(box.toLocaleUpperCase()); //全部转换为大写且本地化

字符串的模式匹配方法
var box = ‘Mr. Lee‘;
//alert(box.match(‘L‘)); //找到字符串则返回这个字符串
//alert(box.match(‘,‘)); //没有找到则返回null
//alert(box.search(‘L‘)); //找到L的位置。
//alert(box.replace(‘L‘,‘Q‘)); //替换掉找到的字符串
//alert(box.split(‘ ‘)); //以空格分割成字符串

其他方法
//alert(String.fromCharCode(76)); //找到asccii码对应的字母

var box = ‘Lee‘;
//alert(box.localeCompare(‘Lee‘)); //1
//alert(box.localeCompare(‘Aee‘)); //0
//alert(box.localeCompare(‘Zee‘)); //-1
//alert(box.localeCompare(‘6576‘)); //1
//alert(box.localeCompare(‘是的‘)); //-1

HTML方法
var box = ‘Lee‘;
alert(box.link(‘http://www.baidu.com‘)); //link添加超链接方法
alert(box.bold()); //加粗方法

JS基本包装类型

标签:操作方法   下标   引用类型   new   替换   arc   box   数字格式化   define   

原文地址:http://www.cnblogs.com/zhengfuheidao/p/6744919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!