JavaScript中的基本包装类型(Boolean类型 Number类型 String类型 Global对象 Math对象)

Boolean类型

var falseObject = new Boolean(false);
var result = falseObject && true;
alert(result);   //true  布尔表达式中所有对象都会被专为true

var falseValue = false;
result = falseValue && true;
alert(result);  //false

alert(typeof falseObject);  //object
alert(typeof falseValue);   //boolean
alert(falseObject instanceof Boolean);  //true
alert(falseValue instanceof Boolean); //false

基本类型的布尔值与Boolean对象的区别

  • typeof操作符对基本类型返回”boolean”,对引用类型返回”object”;
  • instanceof测试Boolean对象返回true,测试基本类型的布尔值返回false。

Number类型

  • 创建
var numberObject = new Number(10);
  • toFixed()方法:按照指定小数位数返回数值的字符串表示
var num = 10;
alert(num.toFixed(2));   //"10.00"
var num = 10.005;
alert(num.toFixed(2));  //10.01  自动舍入
  • toExponential(): 返回以指数表示法(e)表示的字符串形式;
var num = 10;
alert(num.toExponential(1));   //"1.0e+1"
  • toPrecision():返回固定大小(fixed)格式,也可能返回指数(exponential)格式。接收一个表示数字位数的参数(不包括指数部分),看哪种格式最合适,然后输出。
var num = 99;
alert(num.toPrecision(1));    //"1e+2"
alert(num.toPrecision(2));    //"99"
alert(num.toPrecision(3));    //"99.0"

String类型

String类型是字符串的对象包装类型。

  • 创建
var stringObject  = new String("hello world");
  • length属性
var stringValue = "hello world";
alert(string.length);   //"11" 返回字符串中包含字符数
  • 字符方法
    charAt(): 以单字符字符串的形式返回给定位位置那个字符;

    charCodeAt(): 以字符编码字符串的形式返回给定位位置那个字符。

var stringValue = "hello world";
alert(stringValue.chartAt(1));   //"e"
alert(stringValue.charCodeAt(1));  //"101"
  • 字符串操作方法
    • conact():用于讲一或多个字符串拼接起来,返回拼接得到的新字符串。
      实践中多用(+)操作符
var stringValue = "hello ";
var result = stringValue.concat("world","!");//concat 可接收任意多字符串
alert(result);   //"hello world!"
alert(stringValue);  //"hello"
  • slice() 、substr()和substring()

    • 三者第一个参数指定子字符串的开始位置,slice()和substring()第二个参数指定的是子串最后一个字符后面的位置;substr()第二个参数指定返回的字符个数。若不传递第二个参数,默认到字符串末尾。

    • 传递参数为负值时:slice()将传入负值与字符串长度相加;substr()将负的第一个参数加上字符串长度,将负的第二个参数转换为0;substring()将所有负值转换为0。

    var stringValue = "hello world";
    alert(stringValue.slice(3));      //"lo world"
    alert(stringValue.substring(3));  //"lo world"
    alert(stringValue.substr(3));     //"lo world"
    alert(stringValue.slice(3,7));    //"lo w"
    alert(stringValue.substring(3,7));//"lo w"
    alert(stringValue.substr(3,7));   //"lo worl"

    alert(stringValue.slice(-3));    //"rld"
    alert(stringValue.substring(-3));  //"hello world"
    alert(stringValue.substr(-3));    //"rld"
    alert(stringValue.slice(3,-4));   //"lo w"
    alert(stringValue.substring(3,-4));  //"hel"  相当调用substring(0,3)
    alert(stringValue.substr(3,-4));   //""
  • 字符串位置方法

indexOf()和lastIndexOf()

都是从一个字符串中搜索给子字符串,然后返回子字符串的位置。若未找到返回-1。

两个方法都可接收两个参数:搜索子串和开始位置。

区别:indexOf()从头开始,lastIndexOf()从末尾开始。

var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");

while(pos>-1){
    positions.push(pos);
    pos = stringValue.indexOf("e",pos+1)
}
alert(position);   //"3,24,32,35,52"
  • trim()方法
    会创建一个字符串的副本,删除前置及后缀的所有空格。

    返回字符串副本,原始字符串中前置后缀空格保持不变。

var stringValue = "  hello world  ";
var trimmedStringValue = stringValue.trim();
alert(stringValue);  //"  hello world  "
alert(trimmedStringValue);  //"hello world"
  • 字符串大小写转换方法
var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase());//"HELLO WORLD"
alert(stringValue.toUpperCase());    //"HELLO WORLD"
alert(stringValue.toLocaleLowerCase());//"hello world"
alert(stringValue.toLowerCase());  //"hello world"
  • 字符串的模式匹配方法
    match()方法; 在字符串上调用这个方法,本质上与调用RegExp的exec()方法相同。
var text = "cat,bat,sat,fat";
var pattern = /.at/;
var matches = text.match(pattern);
alert(matches.index);  //0
alert(matches[0]);     //"cat"
alert(pattern.lastIndex);//0

search()方法:返回字符串中第一个匹配项的索引;若未找到,返回-1。

var text = "cat,bat,sat,fat";
var pos = text.search(/at/);
alert(pos);    //1

replace()方法
简化替换子字符串操作。第一个参数是RegExp对象或一个字符串,第二个参数可以是一个字符串或函数。

要想替换所有子字符串,需要提供一个正则表达式,而且要设置全局(g)标志。

var text = "cat,bat,sat,fat";
var result = text.replace("at","ond");
alert(result);   //"cond,bat,sat,fat"

result = text.replace(/at/g,"ond");
alert(result);  //"cond,bond,sond,fond"

var text="cat,bat,sat,fat";
result=text.replace(/(.at)/g,"word ($1)");
console.log(result);   //word (cat),word (bat),word (sat),word (fat)

split()方法
可以基于指定的分隔符将一个字符串分割成多个字符串,并将结果放在一个数组中。

var colorText = "red,blue,green,yellow";
var colors = colorText.split(",");      //["red","blue","green","yellow"]
var colors = colorText.split(",",2);    //["red","blue"]
var colors = colorText.split(/[^\,]+/); //["",",",",",",",""]  第一项和最后一项是两个空字符串,是因为正则表达式指定的分隔符出现在了字符串的开头和末尾。
  • localeCompare()方法
    比较两个字符串,按字母表中的顺序返回1或0或-1;
var stringValue = "yellow";
alert(stringValue.localeCompare("brick"));  //1
alert(stringValue.localCompare("yellow"));  //0
alert(stringValue.localCompare("zoo"));     //-1

Global对象

  • URI编码方法
    encodeURI(): 主要用于整个URI进行编码。不会对本身属于URI的特殊字符进行编码,如冒号、正斜杠、问号和井字号。
    encodeURIComponent(): 主要用于URI中的某一段进行编码。会对其发现的任何非标准字符进行编码。
var  uri = "http://www.wrox.com/illegal value.htm#start";
alert(encodeURI(uri));   //http://www.wrox.com/illegal%20value.htm#start
alert(encodeURIComponent(uri));   //http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start
  • URI解码方法
    decodeURI():只能对使用encodeURI()替换的字符进行解码。
    decodeURIComponent():能够解码使用encodeURIComponent()编码的所有字符。

var uri="http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";
alert(decodeURI(enUri));//http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start

alert(decodeURIComponent(enUri));//http://www.wrox.com/illegal value.htm#start
  • eval方法
    接收一个参数,即要执行的ES字符串。
var msg = "hello world";
enval("alert(msg)");  //"hello world"

能够解析代码,功能强大,但同时也非常危险。特别,用户输入数据,可能会恶意输入威胁站点或应用程序安全地代码。

Math对象

  • Math对象属性
alert(Math.E);//自然对数的底数,即常量e的值
alert(Math.PI);//π的值
  • min()和max()方法
var max = Math.max(3,54,32,16);
alert(max); //54

var min = Math.min(3,54,32,16);
alert(min);  //3

var max2=Math.max.apply(Math,values);//把Math对象作为apply()的第一个参数,从而正确设置this值
alert(max2);//8
  • 舍入方法
    Math.ceil() 向上舍入
    Math.floor() 向下舍入
    Math.round() 执行标准舍入 即四舍五入
alert(Math.ceil(25.9)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.1)); //26

alert(Math.floor(25.9)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor(25.1)); //25

alert(Math.round(25.9)); //26
alert(Math.round(25.5)); //26
alert(Math.round(25.1)); //25
  • random()方法
    Math.random()返回大于0小于1的一个随机数。可利用其随机显示一些名人名言和新闻事件。
    公式:值 = Math.floor(Math.random() * 可能值的数 + 第一个可能值)
var num = Math.floor(Math.random() *10 + 1);//1-10之间随机数

猜你喜欢

转载自blog.csdn.net/gaoshanyangzhi_1999/article/details/81206847