JavaScript基础知识二(内置对象)

内置对象

  1.数组对象

    1)数组的声明

      ①方式一:

        var arr=new Array();  //声明一个数组

        arr[0]=100;  //向数组中添加值

        arr[1]=200;

        //arr['name']="autumn";  //JS的数组不支持关联数组

        document.write(arr);  //100,200

        document.write(arr.length);  //打印数组的元素个数

        document.write(arr[0]);  //打印数组的某一个元素

      ②方式二:

        var arr=new Array(100,200,300);

        document.write(arr);  //100,200,300

      ③方式三:

        var arr=new Array(3);  //至少有3个数组元素

        arr[0]=100;

        document.write(arr);  //100,,

      ④方式四:(推荐使用)

        var arr=[100,200,300];

        document.write(arr);  //100,200,300

    2)数组的属性

      array.constructor  对象的类型

      array.length  数组的元素个数

      Array.prototype.成员属性/成员方法=属性值/function(){}  为内置数组对象添加新的属性或方法

    3)数组的方法

      concat()  连接两个或多个数组生成新数组

        arr.concat(arr1,arr2,num1,num2,...)

        参数可以是数组也可以是具体数值

        例:

          var arr1=[100,200,300];

          var arr2=[400,500];

          var arr3=[600,700];

          document.write(arr1.concat(arr2,arr3,800));  //100,200,300,400,500,600,700,800

      join()  将数组元素连接成字符串

        arr.join("指定分隔符")

        参数省略,则默认使用逗号作为分隔符

        例:

          var arr=[100,200,300];

          document.write(arr.join("-"));  //100-200-300

      pop()  删除并返回数组的最后一个元素

        arr.pop()

        例:

          var arr=[100,200,300];

          document.write(arr.pop());  //300

          document.write(arr);  //100,200

      shift()  删除并返回数组的第一个元素

        arr.shift()

        例:

          var arr=[100,200,300];

          document.write(arr.shift());  //100

          document.write(arr);  //200,300

      push()  在数组尾部添加一个或多个元素

        arr.push(element1,element2,...)

        例:

          var arr=[100,200,300];

          document.write(arr.push(400,500));  //5,新的元素个数

          document.write(arr);  //100,200,300,400,500

      unshift()  在数组头部添加元素

        arr.unshift(element1,element2,...)

        例:

          var arr=[100,200,300];

          document.write(arr.unshift(400,500));  //5,新的元素个数

          document.write(arr);  //400,500,100,200,300

      slice()  返回数组中选定部分的元素

        arr.slice(start,end)

        参数start:起始位置,数组头部从0开始,若为负则从尾部开始,-1对应最后一个元素

        参数end:可选参数,结束位置,若为负数则从尾部开始,若未指定则直到数组结束

        例:

          var arr=[100,200,300,400,500];

          document.write(arr.slice(1));  //200,300,400,500

          document.write(arr.slice(2,4));  //300,400

      splice()  删除、替换或插入数组元素

        arr.splice(index,num,val1,val2,...)

        参数index:必需,指定要添加/删除元素的位置,若为负数则从尾部开始

        参数num:必需,要删除的元素数量,为0则不删除

        参数val1,val2,...:可选,向数组添加的新元素

        例:

          var arr=[100,200,300];

          document.write(arr);  //100,200,300

          arr.splice(1,0,666);

          document.write(arr);  //100,666,200,300

          arr.splice(1,3,999);

          document.write(arr);  //100,999

          arr.splice(1,1,200);

          document.write(arr);  //100,200

      reverse()  颠倒数组中的元素顺序

        arr.reverse()

        例:

          var arr=[100,200,300];

          document.write(arr.reverse());  //300,200,100

      sort()  将数组元素排序

        arr.sort(function)

        参数function:可选参数,不指定该参数则按自然顺序排序,若要指定则该参数必须是函数

        例:

          var arr=[10,5,40,25,1000,1];

          document.write(arr.sort());  //1,10,1000,25,40,5

          function sortNumber(a,b){

            return a-b;

          }

          document.write(arr.sort(sortNumber));  //1,5,10,25,40,1000

      toString()  将数组转为字符串

        arr.toString()  与无参数的join()方法返回的字符串相同

        例:

          var arr=[100,200,300];

          document.write(arr.toString());  //"100,200,300"

  2.布尔对象

    true    false

  3.日期对象

    1)日期的创建

      var date=new Date();

      document.write(date);

    2)日期的属性

      date.constructor

      Date.prototype

    3)日期的方法

      getDate()  返回一个月中的第几天(1~31)

        例:

          var date=new Date();

          document.write(date.getDate());

      getDay()  返回一周中的第几天(0~6)

      getMonth()  返回月份(0~11)

        例:

          document.write(date.getMonth()+1);

      getFullYear()  返回四位数字的年份

        例:

          var date=new Date();

          document.write(date.getFullYear());

          var d=new Date("July 21,1990 12:00:00");

          document.write(d.getFullYear());  //1990

      getHours()  返回小时(0~23)

      getMinutes()  返回分钟(0~59)

      getSeconds()  返回秒数(0~59)

        例:

          var date=new Date("July 21,1990 12:00:00");

          document.write(date.getHours());  //12

          document.write(date.getMinutes());  //0

          document.write(date.getSeconds());  //0

      getMilliseconds()  返回毫秒数(0~999)

      getTime()  返回1970年1月1日至今的毫秒数

      parse()  返回1970年1月1日至指定日期(字符串)的毫秒数

        例:

          var date=Date.parse("July 8,2005");

          document.write(date);

      getTimezoneOffset()  返回本地时间与格林威治标准时间(GMT)的时差,以分钟为单位

        例:

          var date=new Date();

          document.write(date.getTimezoneOffset()/60);

    4)计时器      

    <div id="clock"></div>
    <input type="button" id="btn" value="Start">

    <script type="text/javascript">
        window.onload=function(){
            var clock=document.getElementById("clock");
            var getDate=function(){
                var date=new Date();
                //函数体内使用var声明的变量为局部变量
                var strDate=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
                clock.innerHTML=strDate;
            }
            getDate();
            var btn=document.getElementById("btn");
            var n=true;
            btn.onclick=function(){
                if(n){
                    btn.value="Stop";
                    getDate();
                    //函数体内不使用var声明的变量为全局变量
                    //setInterval开始计时器,每隔多少毫秒运行一次
                    interval=setInterval(getDate(),1000);
                    n=false;
                }else{
                    btn.value="Start";
                    //clearInterval清除计数器
                    clearInterval(interval);
                    interval=null;
                    n=true;
                }
            }           
        }

    </script>

  4.数学对象

    1)数学对象属性

      Math.PI  圆周率

    2)数学对象方法

      abs()  返回数的绝对值

        Math.abs(num)

        例:

          document.write(Math.abs(-0.01));  //0.01

      ceil()  进一法取整

        Math.ceil(num)

        例:

          document.write(Math.ceil(0.40));  //1

          document.write(Math.ceil(-5.9));  //-5

      floor()  舍去法取整

        Math.floor(num)

        例:

          document.write(Math.floor(0.60));  //0

          document.write(Math.floor(-5.1));  //-6

      max()  返回最大值

        Math.max(num1,num2,...)

        例:

          document.write(Math.max(5,-3,7,0.5,-1);

      min()  返回最小值

        Math.min(num1,num2,...)

      pow(x,y)  返回x的y次幂的值

        Math.pow(x,y)

      random()  返回0~1之间的一个随机数

        Math.random()

      round()  返回四舍五入后的整数

        Math.round(num)

      sqrt()  返回一个大于等于0的数的平方根

        Math.sqrt(num)

        参数num必须大于等于0

  5.数值对象

    直接使用数字就行,无须使用该对象

    数值对象的方法:

      toFixed()  将数值四舍五入为指定小数位数的数字,不足位数的用0补

        num.toFixed(指定小数位数)

        参数不指定则默认为0,最大实现位数范围为0~20

  6.字符串对象

    1)字符串的属性

      str.length  字符串的长度

      例:

        var str="abc123";

        document.write(str.length);  //6

    2)字符串的方法

      ①字符串的显示

        anchor()  创建HTML锚点

          str.anchor("锚点名称") 

          例:

            var str="Hello world";

            document.write(str.anchor("top"));  //<a name="top">Hello world</a>

        big()  用大号字体显示字符串

        small()  用小号字体显示字符串

          例:

            var str="Hello world";

            document.write(str.big());

            document.write(str.small());

        bold()  用粗体显示字符串

        italics()  用斜体显示字符串

        link()  将字符串显示为超链接

          str.link(url)

        strike()  使用删除线显示字符串

        sub()  将字符串显示为下标

        sup()  将字符串显示为上标

        toLowerCase()  将字符串转换为小写

        toUpperCase()  将字符串转换为大写

        fontcolor()  以指定颜色来显示字符串

          str.fontcolor(color)

          参数color:颜色名、rgb值、十六进制值

        fontsize()  以指定字体大小(1~7)来显示字符串

          str.fontsize(size)

          参数size:1~7的数字

      ②字符串的连接、比较

        concat()  连接两个或多个字符串,作用等同于“+”

          str.concat(str1,str2,...)

          例:

            var str1="Hello";

            var str2="world";

            document.write(str1.concat(str2));  //Hello world

        localeCompare()  比较两个字符串

          str1.localeCompare(str2) 

          若str1小于str2,则返回小于0的数;若str1大于str2,则返回大于0的数;若str1与str2两个字符串相等,则返回0

      ③字符串的查找、匹配

        charAt()  返回字符串中指定位置的字符

          str.charAt(index)

          参数index:必需,指定位置下标,从0开始

          例:

            var str="Hello world";

            document.write(str.charAt(1));  //e

        charCodeAt()  返回字符串中指定位置的字符对应的ASCII码值

          str.charCodeAt(idnex)

        fromCharCode()  由一个或多个ASCII码值返回对应字符组成的字符串

          String.fromCharCode(num1,num2,...)  该方法是String的静态方法

          例:

            document.write(String.fromCharCode(65,66,67));  //ABC

        indexOf()  返回某字符段在字符串中首次出现的位置,未出现则返回-1

          str.indexOf(searchstr,index)

          参数searchstr:必需,要检索的字符段

          参数index:可选,默认从位置0开始检索,指定开始检索的位置,取值范围为0~str.length-1

          例:

            var str="Hello world";

            document.write(str.indexOf("o"));  //4

            document.write(str.indexOf("c"));  //-1

        lastIndexOf()  返回某字符段在字符串中最后出现的位置,未出现则返回-1

          str.lastIndexOf(searchstr,index)

          参数searchstr:必需,要检索的字符段

          参数index:可选,默认从位置0开始检索,指定开始检索的位置,取值范围为0~str.length-1

          例:

            var str="Hello world";

            document.write(str.lastIndexOf("o"));  //7

        match()  在字符串中匹配指定的字符或指定的正则表达式,以数组形式返回匹配到的结果,未匹配到则返回null

          str.match("要匹配的字符")  或  str.match(正则表达式)

          例:

            var str="Hello world! 1 plus 2 equal 3";

            document.write(str.match("world"));  //world

            document.write(str.match("haha"));  //null

            document.write(str.match(/d+/g));  //1,2,3

        replace()  替换掉字符串中与指定的匹配字符或正则表达式相匹配的内容,返回替换后的新字符串

          str.replace("要匹配的字符"/正则表达式,"替换文本")

          例:

            var str="hello everybody,hello world,hello myself!";

            document.write(str.replace("hello","bye"));  //bye everybody,hello world,hello myself!

            document.write(str.replace(/hello/,"bye"));  //bye everybody,hello world,hello myself!

            document.write(str.replace(/hello/g,"bye"));  //bye everybody,bye world,bye myself!

            document.write(str.replace(/w+/g,function(word){return word.substring(0,1).toUpperCase()+word.substring(1);}));

            //Hello Everybody,Hello World,Hello Myself!

        search()  返回字符串中与指定的匹配字符或正则表达式首次匹配的位置,未匹配则返回-1

          str.search("要匹配的字符"/正则表达式)

          例:

            var str="ok,hello everybody,hello world!";

            document.write(str.search("hello"));  //3

            document.write(str.search(/Hello/));  //-1

            document.write(str.search(/Hello/i));  //3

      ④字符串的截取、分割

        slice()  截取字符串

          str.slice(start,end)

          参数start:必需,开始截取的下标位置,若为负数则从尾部开始

          参数end:可选,截取结束的下标位置,若未指定该参数则从start位置截取到字符串结束;若为负数则从尾部开始,该位置对应的字符不被截取

          例:

            var str="Hello world";

            document.write(str.slice(6));  //world

            document.write(str.slice(6,-1));  //worl

        substr()  截取指定长度的字符

          str.substr(start,length)

          参数start:必需,开始截取的下标位置,若为负数则从尾部开始

          参数length:可选,要截取的字符长度,若未指定该参数,则从start位置截取至字符串结束

          例:

            var str="Hello world";

            document.write(str.substr(6));  //world

            document.write(str.substr(6,1));  //w

        substring()  截取字符串

          str.substring(start,end)

          参数start:必需,非负的整数,开始截取的下标位置

          参数end:可选,非负的整数,截取至该位置对应的字符,但不包括该字符;若未指定该参数,则从start位置截取至字符串结束

          例:

            var str="Hello world";

            document.write(str.substring(6));  //world

            document.write(str.substring(6,7));  //w

        split()  分割字符串为字符段数组

          str.split("分隔符",返回的数组的最大长度)

          参数"分隔符":必需,为字符串或正则表达式

          参数“返回的数组的最大长度”:可选,若未设置该参数则返回所有分割后的字符段;若指定该参数,则返回的字符段数组元素不会多于这个指定数

          例:

            var str="Hello world";

            document.write(str.split(" "));  //Hello,world

            document.write(str.split(""));  //H,e,l,l,o, ,w,o,r,l,d

            document.write(str.split(" ",1));  //Hello

            document.write(str.split("",1));  //H

  7.正则表达式对象

    /正则表达式/修饰符    (注意:正则表达式不加引号

    1)修饰符

      i  不区分大小写地匹配

      g  全局匹配(查找所有匹配而非在找到第一个匹配后就停止)

      m  执行多行匹配

    2)正则表达式的方法

      exec()  检索字符串中与正则表达式相匹配的值,以数组形式返回匹配到的结果,未匹配到则返回null

        例:

          var str="Hello world";

          var pattern=/world/g;

          document.write(pattern.exec(str));  //world

          注意:exec()在完成一次匹配后指针会向后移动到匹配位置,若要接着开始重新检索字符串,须手动将 lastIndex 属性重置为0

      test()  检索字符串中与正则表达式相匹配的值,返回true或false

        例:

          var str="Hello world";

          var pattern=/world/g;

          document.write(pattern.test(str));  //true

原文地址:https://www.cnblogs.com/zhouwanqiu/p/9709858.html