js的內部類


JavaScript中本身提供一些,可以直接使用的類,這種類就是內部類。主要有:

Object/Array/Math/Boolean/String/RegExp/Date/Number共8個內部類。

內部類的分類:

從使用方式,把js內部類分為兩類(動態類,靜態類)。

靜態類 使用  類名.屬性|方法

比如Math

動態類 使用  var 對象=new 動態類() 對象.屬性|方法

//Math
    window.alert(Math.abs(-12));
    //Date
    //顯示當前的日期
    var nowdate=new Date();
    window.alert(nowdate.toLocaleString());//按照一定格式顯示。

Math常用的函數

1.abs(x)返回數的絕對值

2.ceil(x)對一個數進行上舍入

3.floor(x)對一個數進行下舍入

4.max(x,y)求x,y中較大的數

5.min(x,y)求x,y中較小的數

6.round(x)對x進行四舍五入

7.random()一個大於0小於1的16位小數位的數字。

window.alert(Math.abs(-12));
    //Date
    //顯示當前的日期
    var nowdate=new Date();
    window.alert(nowdate.toLocaleString());

    alert(Math.ceil(4.5));
    alert(Math.floor(4.5));
    alert(Math.round(4.7));
    alert(Math.round(4.49));
    alert(Math.random());

    alert(parseInt(Math.random()*100));
    alert(Math.round(Math.random()*100));

js內部類--Date類

//Date的常用方法
    //window.alert(new Date().toLocaleString());

    /*var date=new Date();
    window.alert(date);
    window.alert(date.getYear()+" "+date.getMonth());*/

    function showHello(date){
        //小時
        var hour=date.getHours();
        //分鍾
        var minute=date.getMinutes();

        //1. 思路  把小時和分鍾轉成 距離00:00的秒數(小時數)
        //alert(new Date().toLocaleString());
        /*alert(hour);
        alert(minute);*/

        //把秒數考慮進去
        var mysecond=hour*3600+minute*60;

        if(mysecond>=6*3600&&mysecond<=9*3600){
            window.alert("早上好");
        }else if(mysecond>=17*3600+31*60&&mysecond<=18*3600+40*60){
            window.alert("傍晚好");
        }

        /*var nowTime=date.getHours()+date.getMinutes();
        if((nowHour>=6&&nowHour<9)||(nowHour==9&&minute==0)){
            window.alert("早上好");
        }else if(nowHour>=6&&nowHour<9){

        }*/

String類

String是動態類,提供了對字符串的各種操作,下面介紹幾個常用的函數。

split();分割字符串。

substr(start,length):從位置start開始截取length個字符

substring(start,end):從位置start開始截取到字符直到end-1結束。

var str="abcd12345";
    window.alert(str.length);
    var str2="abc def oop";
    var arr=str2.split(" ");//如果是split("")則按字符分割。
    window.alert(arr);

    var str3="abcdef";
    window.alert(str3.substr(2,3));//cde
    window.alert(str3.substring(2,3));//c

    var str4="abcd";
    window.alert("charAt"+str4.charAt(3));

運行結果:

abc def oop

cde

c

d

indexOf的用法

indexOf(str,start)

從位置start開始尋找字符串str,默認情況下start是0(不寫的時候),如果找到則返回字符串出現的位置,否則返回-1.

String是一個動態類。

var str5="ab 123 56 123 ab";
window.alert("position:"+str5.indexOf("ab",1));//支持漢字查詢

返回14.

Array類
是動態類
看看常用的方法:

pop()刪除一個元素並返回最后一個元素

push()添加一個元素

var myarr=new Array();
    window.alert(myarr.length);
    //動態地添加數據
    myarr[0]="sp";
    myarr[1]=90;
    window.alert(myarr.length+" "+myarr);
    //刪除數組中的元素
    myarr.pop();
    window.alert(myarr.length+" "+myarr);
    myarr.push("abcd");
    window.alert(myarr.length+" "+myarr);

輸出結果:0

2 sp,90

1 sp

2 sp,abcd

 
         

var arr=new Array(2);
window.alert(arr.length);
arr[0]=1;
arr[1]="a";
arr[2]=2.3;
window.alert(arr.length);
window.alert(arr[3]);
//arr[10]="hello";//不要跳過下標賦值,雖然不會報錯並且在第10個位置插入了元素,但是不利於程序
arr['a']="ok";
window.alert(arr.length+" "+arr);
window.alert(arr['a']);

 

輸出:

2

3

undefined

3 2,3,undefined

ok

雖然有arr['a']="ok";這條語句插入了一個元素,但是數組元素個數卻沒有增加。

js中允許這樣訪問變量

function Person(){
        this.name="xiao";
        this.show=function(){
            window.alert("hello");
        }
    }
    var p=new Person();
    document.writeln(p.name);
    document.writeln(p["name"]);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM