JavaScript内置对象--基本包装类型(Boolean、Number、String)详解

来源:互联网 发布:盗取qq密码软件 编辑:程序博客网 时间:2024/04/28 12:52

一、什么是基本包装类型?

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

二、其他内置对象与基本包装类型对象的区别?

普通的内置对象与基本包装类型的主要区别就是对象的生命期使用new操作符创建的引用类型的实例,在执行流离开当前作用域之前都一直保存在内存中,而自动创建的基本包装类型的对象,则只是存在于一行代码的执行瞬间,然后立即被立即销毁。这意味着我们不能再运行时为基本包装类型值添加属性和方法。

var s1 = "some text";
s1.color = "red";
alert(s1.color) ;  //undefined


在第二行为s1添加一个color属性,第三行代码执行时,再次访问s1,结果s1的color属性被销毁了

Boolean类型

Boolean类型是与布尔值对应的引用类型,要创建Boolean对象,可以像下面这样调用Boolean构造函数并传入true或者false值。




其实,Boolean对象在实际开发中用处不大,因为它很容易造成人们的误解。其中最常见的问题就是布尔表达式中使用Boolean对象。

例:
var a= new Boolean(false);
var b = a && true;
console.log(b);    //true
b = a && true;
console.log(b);  //false

案例中,很多人觉得第三行代码是false,但实际是true,因为在布尔表达式中所有对象都会转为true,因此a在布尔表达式中代表true,true && true当然结果是true,第四行代码就是进行普通的逻辑运算了,返回false



Number类型

Number类型是数字对应的引用类型,要创建Number对象,可以在调用Number构造函数时向其中传递相应的数值。



Number类型的几个方法:
1.toFixed():把数字转换为定点表示法表示的字符串,并具有指定的小数位数
注意:toFixed()方法只可以表示带有0~20个小数位数的数值。

var num=10;
console.log(num.toFixed(2));  //10.00

var num2=10.005;
console.log(num.toFixed(2));  //10.01

2.toExponential(): 把数字转换为指数计数法表示的字符串,并具有指定的小数位数

var num=10;
console.log(num.toExponential(1));   //1.0e+1;


3.toPrecision():把数字格式化为具有指定有效位的字符串

var num=99;
console.log(num.toPrecision(1));  //1e+2
console.log(num.toPrecision(2));  //99
console.log(num.toPrecision(3));  //99.0

4.toString():把数字转换为指定进制(默认十进制)表示的字符串

var num=10;
console.log(num.toString(2));  //转换为2进制,结果为:1010





String类型

一、字符方法
1. str.charAt()  :以单字符串形式返回特定位置的那个字符

var stringValue = "hello world";
console.log(stringValue.charAt(1));   //e

2. str.charCodeAt() : 返回特定位置的字符编码

var stringValue = "hello world";
console.log(stringValue.charCodeAt(1));  //101

二、字符操作方法
1. str.concat() : 用于将一个或多个字符串拼接起来,返回拼接得到的新字符串

var stringValue= "hello";
var result=stringValue.concat("world");
console.log(result);   //hello world


2. str.slice()/str.substring()/str.substr() : 这个三个方法都返回操作字符串的一个子字符串,而且都接受一或两个参数,第一个参数指定子字符串开始的位置,str.slice()与str.substring()第二个参数表示子字符串到哪里结束,如果第二个参数省略,则默认是结尾结束,str.substr() 第二个参数表示返回的字符个数

var stringValue = "hello world";
console.log(stringValue.slice(3));    //lo world
console.log(stringValue.substring(3));   //lo world
console.log(stringValue.substr(3));   //lo world
console.log(stringValue.slice(3,7));  //lo w
console.log(stringValue.substring(3,7));  //lo w
console.log(stringValue.substr(3,7));  //lo worl    返回字符个数7个


三、字符串位置方法
1. indexOf()/lastIndexOf() : 从一个字符串中搜索给定的子字符串,然后返回子字符串的位置(如果没有找到,返回-1),这个两个方法的区别在于: indexOf()方法从字符串的开头向后搜索子字符串,而lastIndexOf()方法是从字符串的末尾向前搜索子字符串,然后这两个方法可以接受第二个参数,表示从字符串中的哪个位置开始搜索。

var  stringValue = "hello world";
console.log(stringValue.indexOf("o"));   //4
console.log(stringValue.lastIndexOf("o"));   //7
console.log(stringValue.indexOf("o",6));   //7
console.log(stringValue.lastIndexOf("o",6));   //7


四、trim()方法
1. str.trim() : 创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果

var stringValue = "   hello  world     ";
var trimString = stringValue.trim();  
console.log(stringValue);   //"   hello  world     "
console.log(trimString);     //"hello  world"


五、字符串大小写转换方法
1. str.toLocaleUpperCase() : 将所有大写字母转换为小写

var stringValue =  "HELLO WORLD";
var LocaleString=stringValue.toLocaleUpperCase();
console.log(stringValue);  //"HELLO WORLD"
console.log(LocaleString); //"hello world"


2. str.toUpperCase() : 将所有小写字母转换为大写

var stringValue = "hello world";
vae UpperString= stringValue.toUpperCase();
console.log(stringValue);  //"hello world"
console.log(UpperString);  //"HELLO WORLD"


六、字符串的模式匹配方法
1.str.search() : 这个方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,由开头向后查找,返回字符串中第一个匹配项的索引,如果没有找到返回-1,这个方法专用于查找关键词位置。

var stringValue="cat, bat, sat ,fat";
var searchString = stringValue.search(/.at/);
console.log(searchString); //1

2.str.match() : 这个方法可在字符串内检索指定的值,或找到一个或多个与正则表达式匹配的字符,返回值是包含所有子字符串的数组,如果没有找到,返回null吗,这个方法专用于查找关键词的内容。

相应的格式有:
str.match(/正则表达式/);  仅查找第一个出现关键词的内容,区分大小写
str.match(/正则表达式/i);   仅查找第一个出现关键词的内容,不区分大小写
str.march(/正则表达式/g);  查找所有关键词的内容,区分大小写
str.march(/正则表达式/ig);  查找所有关键词的内容,不区分大小写
注意:  i是忽略大小写, g指的是全局

var stringValue="cAt, bat, sAt, fat";
var matchString1=stringValue.match(/.at/);
var matchString2=stringValue.match(/.at/i);
var matchString3=stringValue.match(/.at/g);
var matchString4=stringValue.match(/.at/ig);

console.log(matchString1);  //"bat"
console.log(matchString2);  //"cAt"
console.log(matchString3);  //"bat, fat"
console.log(matchString4);  //"cAt, bat, sAt, fat"


3.  str.replace() : 这个方法用于在字符串中用一些字符替换特定的字符,或替换一个与正则表达式匹配的字符,这个方法可以接受两个参数,第一个参数是字符串或者是正则表达式,第二个参数可以是一个字符串或者一个函数,如果第一个参数是字符串,那么只会替换第一个子字符串,如果第一个参数是正则表达式,则可以替换全部子字符串,前提是指定全局(g)标志

var stringValue="cat, bat, sat, fat";
var replaceString1=stringValue.replace("at","ond");
var replaceString2=stringValue.replace(/at/g,"ond");
console.log(replaceString1);  //"cond, bat, sat ,fat"   第一个参数是字符串,所以只替换第一个子字符串
console.log(replaceString2);  //"cond, bond, sond, fond"   第一个参数是/正则表达式/g,  所有替换全部子字符串


七、localeCompare()方法
1.  str.localeCompare(): 这个方法用来比较两个字符串,如果字符串在字母表中应该排在字符串参数之前,则返回-1,如果等于字符串参数,则返回0,如果排在字符串参数之后,则返回1,通常与参数首字母比较

var stringValue="yellow";
console.log(stringValue.localeCompare("zoo"));   //排在参数之前,返回-1
console.log(stringValue.localeCompare("year"));   //等于字符串参数,返回0
console.log(stringValue.localeCompare("brick"));   //排在参数之后,返回1


八、fromCharCode()方法
1.str.fromCharCode(): 这个方法是接收一个或多个字符编码,然后将他们转换成字符串,从本质上看,这个方法执行的是与charCodeAt()相反的操作

var stringValue = "104,101,108, 108 ,111";
console.log(String.(stringValue.fromCharCode));  //"hello"


九、HTML方法
早期的Web浏览器提供商差距了使用JS动态格式化HTML的需求,于是这些提供商就扩展了这些标准,实现了一些专门用于简化常见HTML格式化任务的方法。不过,需要注意的是,尽量不要使用这些方法,因为他们创建的标记通常无法表达语义

anchor(name)
big()
bold()
fixed()
fontcolor(color)
fontsize(size)
italics()
link(url)
small()
strike()
sub()
sup()

由于这些方法不经常使用,我就不一一列举了,感兴趣的同学可以自行学习。



0 0
原创粉丝点击