zoukankan      html  css  js  c++  java
  • js常用内置对象

    1、概念

        内置对象是由ECMAScript实现并提供独立于宿主环境的对象。
    2、分类
      Array、String对象、Math对象、Number对象、Boolean对象、Regexp对象

    3、具体讲解

    =======内置对象:String======  
        1、创建字符串
            new创建:var str=new String("aa");
             直接创建:var str="";
        2、字符串的方法
            indexOf()  第一个出现的位置
            charAt()  根据下标获取元素
            toUpperCase()  变大写
            tolowerCase()变小写
            截取子串 :substring()  slice() 

         3、案例

    //从字符串“wang zhan gong cheng”中提取“gong cheng”子串,并将该字串的首字母大写。 Gong cheng
    var str="wang zhan gong cheng";
    //获取"gong cheng"第一次出现的位置
    var index=str.indexOf("gong cheng");
    //alert(index);
    //根据下标截取子串
    var newstr=str.substring(index);
    //将子串的首字母变大写
    var end=newstr.charAt(0).toUpperCase()+newstr.substring(1);
    alert(end);  //Gong cheng

    =======内置对象:Math====== 

     1、概念   Math对象是封装了数学公式和信息的内置对象

     2、常用的方法

           abs(x)    返回数值的绝对值
           ceil(x)    对数值进行向上取整
      floor(x)    对数值进行向下取整
      round(x)    对数值进行四舍五入取整
      max(x,y,...)    返回多个数中的最大值
      min(x,y,...)    返回 多个数中的最小值
      pow(x,y)    返回 x 的 y 次幂
      random()    返回 0 ~ 1 之间的随机数(含0不含1) 

      3、案例

    //比较1~100之间的两个随机整数的大小
    /*
     *  
      Math.random()  [0,1)
      Math.random()*100  [0,100)
      Math.random()*100+1   [1,101)
      Math.floor( Math.random()*100+1)  [1,100]
    */
    var num1=Math.floor( Math.random()*100+1);
    var num2=Math.floor( Math.random()*100+1);
    //Math.max(求数的大小)
    alert("num1="+num1+",num2="+num2);
    alert("最大值为:"+Math.max(num1,num2));

    =======内置对象:Date======

    1、概念

            Date 对象是封装了日期相关属性和操作的内置对象

    2、创建对象

        1、无参,返回当前日期时间   语法格式:var 变量名=new Date()

        2、有参,传入毫秒数   语法格式:var 变量名=new Date(milliseconds)  得到1970年1月1日8点过多少毫秒的日期时间对象
        3、把字符串转换为Date对象  语法格式:var 变量名=new Date(dateStr)

           注意字符串的类型应该是固定的如:2017/09/09或2017_09_09

    3、常用方法
            getFullYear()     返回Date对象的4位年份值
            getMonth()    返回Date对象的月份值。从0开始,所以真实月份=返回值+1 [0,11]
            getDate()    返回Date对象的月份中的日期值;值的范围1~31
            getHours()    返回Date对象的小时值
            getMinutes()    返回Date对象的分钟值
            getSeconds    返回Date对象的秒数值
            getMilliseconds    返回Date对象的毫秒值
            getDay()    返回Date对象的一周中的星期值;0为星期天,1为星期一,依此类推    [0,6]

    4、基本案例

    =====================显示当前时间===========================
      var y=date.getFullYear();
       //alert(y);//2020
       var m=date.getMonth()+1;
       var d=date.getDate();
       //alert(d);
       var h=date.getHours();
       var mm=date.getMinutes();
       var s=date.getSeconds();
       alert(y+"年"+m+"月"+d+"日 "+h+":"+mm+":"+s)
    ====================根据时间显示问候语========================
    /* 定义一个一维数组,存储5个问候语:上午好,中午好,下午好,傍晚好,晚上好
    创建一个表示当前时间的Date对象。
    在页面上打印输出当前日期时间
    判断当前所处时间段显示相应的问候语
    上午8:00—11:00;中午11:00—13:00;下午13:00—17:00;傍晚17:00—19:00,晚上19:00—24:00
     */
     var arr=["上午好","中午好","下午好","傍晚好","晚上好"];
     //定义一个下标
     var index=0;
     var date=new Date();
     //将对象变成字符串
    //alert(date.toLocaleString());
     var h=date.getHours();
     if(h>=8&&h<11){
         index=0;
     }else if(h>=11&&h<13){
         index=1;
     }else if(h>=13&&h<17){
         index=2;
     }
     else if(h>=17&&h<24){
         index=3;
     }else{
         index=4;
     }
     //定义变量,保存日期时间和问候语
     var str=date.toLocaleString()+"
    "+arr[index];
     alert(str);
     

    =======内置对象:Number======

    1、概念

    Number对象是与数值相关的内置对象
    2、常用属性和方法

           属性:

           MAX_VALUE    可表示的最大数
      MIN_VALUE    可表示的最小数
      NaN    非数字值
      POSITIVE_INFINITY    正无穷大
      NEGATIVE_INFINITY    负无穷大

          方法:toString()和toFixed()方法

     //1、toString(a)  将参数变成相应进制的字符串,无参默认10进制
      /* 
    var a=10; alert(a.toString(2)); //1010 alert(a.toString(8));//12 alert(a.toString(16));//a alert(a.toString(10));//10 alert(a.toString()+":"+typeof a.toString() );//10 string
    */ //2、toFixed() 截取小数点后几位,得到的结果也是字符串类型,无参默认整数 /* var num1=10.493; var num2=10.515; var num3=10; console.log(num1.toFixed());//10 console.log(num2.toFixed());//11 console.log(num3.toFixed());//10 console.log(num1.toFixed(2));//10.49 console.log(num2.toFixed(2));//10.52 console.log(num3.toFixed(2));//10.00 console.log(typeof num1.toFixed());//string */

    =======内置对象:Boolean======

    1、概念

        Boolean对象是封装了逻辑值的内置对象

    2、 new  Boolean(变量名) 将所有的变量变成true或false,
               变成false的变量有0、false、null、"",NaN,undefined

      

  • 相关阅读:
    在ubuntu9中安装配置java postgresql pljava tsearch2
    Zend中配置Memcached
    PHP 截取字符串专题(转)
    网络执法官.P2P终结者等监控网络流量控制软件的原理和防范
    C++中的浮点数格式控制
    HTML教程
    网络执法官.P2P终结者等监控网络流量控制软件的原理和防范
    CodeGear RADstudio 2007+Sp3下载地址及破解
    HTML教程
    CodeGear RADstudio 2007+Sp3下载地址及破解
  • 原文地址:https://www.cnblogs.com/sunflying/p/13197893.html
Copyright © 2011-2022 走看看