[Javascript 高级程序设计]学习心得记录8 引用类型(下)

来源:互联网 发布:全面战争模拟器 mac 编辑:程序博客网 时间:2024/06/06 09:28

昨天讲了基本对象类型和数组类型,今天继续讲RegExp类型,函数类型和基本包装类型。

一,RegExp类型

    这个类型就是正则表达式的类型(不是很准确,暂时先这么理解吧),正好在这把正则表达式再复习一遍。正则表达式格式是 /pattern/flags,其中pattern是匹配用的正则表达式,flags是标志,用以标明正则表达式的行为,有3种:g:全局模式,应用于全部的字符串,而非在发现第一个匹配值时停止。 i:不区分大小写模式匹配。m:多行模式。与其他语言中的正则表达式类似,模式中使用的元字符都需要加转义字符\。而使用RegExp类型时,也是用new,接两个参数,模式和标志,所有模式中使用的元字符都需要加双重转义。

1.正则类型实例属性

    正则类型每个实例都有五个属性,分别表示g,i,m,模式的值和下一次开始搜索的位置。

2.正则类型实例方法

    主要是两个:exec(),test()。exec()接收要匹配的字符串,返回包含第一个匹配信息的数组,如果没有匹配项返回null。而返回的这个数字,不仅包含匹配到的字符串,而且有两个特别的属性:index表示匹配字符串所在位置,input就是被匹配的这个字符串。

        var text = "mom and dad and baby";                var pattern = /mom( and dad( and baby)?)?/gi;        var matches = pattern.exec(text);                alert(matches.index);    //0        alert(matches.input);    //"mom and dad and baby"
    值得注意的是,即使设置了全局标志g,exec每次也只会返回一个匹配项,再执行才会返回之后的。如果没有设置全局标志,那么每次调用都只返回第一个匹配项。

        var text = "cat, bat, sat, fat";                var pattern1 = /.at/;                var matches = pattern1.exec(text);                alert(matches.index);    //0        alert(matches[0]);       //"cat"        alert(pattern1.lastIndex);//0        matches = pattern1.exec(text);                alert(matches.index);    //0        alert(matches[0]);       //"cat"        alert(pattern1.lastIndex);//0        var pattern2 = /.at/g;                var matches = pattern2.exec(text);                alert(matches.index);    //0        alert(matches[0]);       //"cat"        alert(pattern2.lastIndex);//0        matches = pattern2.exec(text);                alert(matches.index);    //5        alert(matches[0]);       //"bat"        alert(pattern2.lastIndex);//0
    此外还有test()方法,返回是否匹配布尔值,用于判断。

    其实还有RegExp构造函数属性,不过我感觉没什么用,就不说了。

二,函数类型

    函数是对象,函数名是指针。js函数没有重载,可以作为值返回,此外还有很多东西,会在下几篇博客提到。这里就先简单介绍,下一篇会深度挖掘。

        function sum(num1, num2){            return num1 + num2;        }//函数的3种定义方式,推荐这种        alert(sum(10,10));    //20                var anotherSum = sum;                alert(anotherSum(10,10));  //20                sum = null;                alert(anotherSum(10,10));  //20
    函数内部属性有arguments和this,this就不说了,很其他语言的差不多。arguments在第一篇说过了http://blog.csdn.net/m0_37645820/article/details/77749799。

    函数的属性有length,接收参数的个数。还有prototype,这个后面会专门说。

    函数的方法有apply()和call(),主要作用是改变函数的作用域。

        window.color = "red";        var o = { color: "blue" };                function sayColor(){            alert(this.color);        }                sayColor();            //red                sayColor.call(this);   //red        sayColor.call(window); //red        sayColor.call(o);      //blue
三,基本包装类型
    为了方便基本类型的操作,js有三种特殊的引用类型:布尔,数字和字符串。实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据。与其他引用类型的区别就是,自动创建的基本包装类型的对象,只存在于一行代码的执行瞬间,然后立即销毁。所以我们不可能在运行时为基本类型值添加属性和方法。其中布尔类型感觉没什么用,就不说了。

1,数字类型

    数字类型重写了toSrting()方法,可以按进制输出数值的字符串形式。增加了toFixd方法修改位数。

        var numberObject = new Number(10);        var numberValue = 99;                //toString() using a radix        alert(numberObject.toString());       //"10"        alert(numberObject.toString(2));      //"1010"        alert(numberObject.toString(8));      //"12"        alert(numberObject.toString(10));     //"10"        alert(numberObject.toString(16));     //"a"                //toFixed()        alert(numberObject.toFixed(2));    //outputs "10.00"        numberObject = new Number(99);        alert(numberObject.toPrecision(1));    //"1e+2"        alert(numberObject.toPrecision(2));    //"99"        alert(numberObject.toPrecision(3));    //"99.0"                   alert(typeof numberObject);   //object        alert(typeof numberValue);    //number        alert(numberObject instanceof Number);  //true        alert(numberValue instanceof Number);   //false
2,字符串类型
    这个才是重头戏。在c语言中字符串就是字符数组,在js也可以这么理解,字符串类型继承很多数组的属性和方法。像length,concat(),slice(),indexOf()等,这些就不说了,下面说些特别的。

    trim()方法,返回去掉前后空格的字符串副本,非常实用。

    字符串大小写转换方法:

        var stringValue = "hello world";        alert(stringValue.toLocaleUpperCase());  //"HELLO WORLD"        alert(stringValue.toUpperCase());        //"HELLO WORLD"        alert(stringValue.toLocaleLowerCase());  //"hello world"        alert(stringValue.toLowerCase());        //"hello world"
   还有一个很实用的方法split(),用于分割字符串

   三种字符串的模式匹配方法:match() search() replace()。前两个是查找,后一个是替换。

        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        var pos = text.search(/at/);        alert(pos);   //1        var result = text.replace("at", "ond");        alert(result);    //"cond, bat, sat, fat"        result = text.replace(/at/g, "ond");        alert(result);    //"cond, bond, sond, fond"        result = text.replace(/(.at)/g, "word ($1)");        alert(result);    //word (cat), word (bat), word (sat), word (fat)                function htmlEscape(text){            return text.replace(/[<>"&]/g, function(match, pos, originalText){                switch(match){                    case "<":                        return "<";                    case ">":                        return ">";                    case "&":                        return "&";                    case "\"":                        return """;                }                         });        }                alert(htmlEscape("<p class=\"greeting\">Hello world!</p>")); //<p class="greeting">Hello world!</p>        var colorText = "red,blue,green,yellow";        var colors1 = colorText.split(",");      //["red", "blue", "green", "yellow"]        var colors2 = colorText.split(",", 2);   //["red", "blue"]        var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]
四,Global对象

    所有在全局作用域定义和函数和属性都是全局对象的函数和属性。

    1,URI编码方法

    直接上代码吧:

        var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";                //http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start        alert(decodeURI(uri));                //http://www.wrox.com/illegal value.htm#start        alert(decodeURIComponent(uri));
        var uri = "http://www.wrox.com/illegal value.htm#start";                //"http://www.wrox.com/illegal%20value.htm#start"        alert(encodeURI(uri));                //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"        alert(encodeURIComponent(uri));
    2. eval()方法

    这个方法是很重要的方法,json就靠它解析的。实际上,这就是一个js解析器,接收一个字符串参数,即要执行的js代码的字符串。后面还会细说。8点了,跑步去了。







        

阅读全文
0 0
原创粉丝点击