javascript的內建對象,javascript內建

來源:互聯網
上載者:User

javascript的內建對象,javascript內建

         我們已經知道了對象是什麼,如何建立對象以及如何使用對象.現在給大家介紹一下javascript中一些常用的內建對象.(String對象,Math對象,Number對象,Array對象和Date對象)

 

String對象

 

String類型一些逸出字元.

 

//建立一個String對象var string1 = newString("hello");var string2 = newString(123);var string3 = newString(1.23); var box = 11;var box = true;alert(typeofbox.toString());//string,toString()方法可以把值轉換成字串。

/*toString()方法一般是不需要傳參的,但在數值轉換為字串的時候,可以傳遞進位參數*/var box = 10;alert(box.toString());//10,預設十進位輸出alert(box.toString(2));//1010,二進位輸出
 

//charAt()方法和charCodeAt()方法--從字串中選取一個字元/*prompt()函數顯示一個提示框,請求使用者輸入一個字串,如果使用者未輸入,預設值為"helloworld!"*/var myString =prompt("enter some text","hello world!");/*charAt()函數擷取字串中的指定位置(最後)的單個字元.索引位置從0開始*/var thelastword =myString.charAt(myString.length-1);/*與charAt()方法類似,charCodeAt()方法返回的是特定位置的單個字串的unicode編碼*/

Math對象

 

 

 

/*取捨方法*/ /*向上舍入*///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.floor(Math.random()*總數+第一個數值)*///alert(Math.floor(Math.random()*10+1));//1-10之間的任意數 /*傳遞範圍函數*///functionselectFrom(lower, upper) {//    var sum = upper - lower + 1;//    return Math.floor(Math.random() * sum +lower);//}//for (var i = 0; i <10; i++) {//    document.write(selectFrom(5, 10));//    document.write('<br/>');//}

一些其他方法:

 

 

Number對象

var box = 250;//十進位整數alert(box); var box =070;//八進位整數,十進位輸出56alert(box); var box = 3.80;//浮點型alert(box); var box = 12.0;//自動轉換alert(box); var box = 4.12e3;//科學技術法alert(box); var box =0.000000000412;//科學技術法alert(box); var box =100e10000;//超過範圍alert(box); var box = 100e10000;alert(isFinite(box));//isFinite函數檢查是否超出範圍,返回false或者true

/*NaN,即非數值(Not aNumber)是一個特殊的值,這個數值表示一個本來要返回數值的運算元未返回數值的情況(這樣就不會報錯了)。*///var box = 0 / 0;//NaN//alert(Number.NaN)//通過Number.NaN得到NaN值//alert(NaN+1)//任何與NaN進行運算的結果均為NaN//alert(NaN ==NaN)//NaN不與自身相等(NaN不與任何值相等)/*isNaN()函數用來判斷這個值到底是不是NaN。isNaN函數接收到一個值後會嘗試將其轉化為數值*///alert(isNaN(NaN))//true//alert(isNaN(25))//false

/*有3個函數把非數值轉換為數值:Number()、parseInt()和parseFloat()。Number()適用於任何類型,後兩個專門用於將字串轉換成數值。*///alert(Number(true));//1,boolean類型的true和false分別轉換成1和0//alert(Number(25));//25,數值型直接返回//alert(Number(null));//0,Null 物件返回0//alert(Number(undefined));//NaN,undefined返回NaN//alert(parseInt('456lee'));//456,會返回整數部分//alert(parseInt('lee456lee'))//NaN,第一個不是數值就會返回NaN//alert(parseInt('12lee34lee'))//12,從第一個數值開始,到最後一個連續數值//alert(parseInt('12.34'))//12,小數點不是數值,去掉//alert(parseInt(''))//NaN,空返回NaN

parseFloat()是用於浮點數值轉換的,用法和parseInt()一樣,從第一位解析到非浮點數值位置,只認一個小數點,把科學計數法轉換為普通值。

 

Array對象

/*concat()方法--串連兩個數組,形成一個新的數組.*/var names = newArray("張三", "李四", "王五");var ages = new Array(31,29, 34); var concatArray; concatArray =names.concat(ages);
圖1


圖2


圖2是圖1中的圖合并後的效果

/*slice()方法--擷取數組的部分元素*/var names = newArray("張三", "李四", "王五");//將把數組的元素複製給新數組slicearray,新數組索引為0//取出的應是"張三",這是一種左閉右開即[)規則.var slicearray =names.slice(1, 2);

/*join()方法--將數群組轉換為字串,並將這些字串串連成一個完整的字串.join()方法只有一個參數,就是數組元素之間串連成字串時的分隔字元*///定義一個數組對象myShoppingvar myShopping = newArray("蘋果", "香蕉", "橘子");//使用join()方法將數組的元素轉換成一個字串:"蘋果<br>香蕉<br>橘子"var list =myShopping.join("<br>");//document.write()方法列印出來document.write(list);

其他常用方法:

Sort()方法--對數組進行排序

Reverse()方法--反轉數組中元素的順序

 

Date對象

 

獲得Date對象的值

設定Date對象的值

 

//建立了一個日期對象,構造方法裡面可以傳參數,指定時間,如果沒有傳,就是預設當前的時間var box = newDate();                        alert(box);
<span style="font-family: 宋體; font-size: 14pt;">//返回的是一個毫秒數1176307200000</span>

alert(Date.parse('4/12/2007'));                         //把毫秒數轉換成看的懂的時間var box = newDate(Date.parse('4/12/2007'));        alert(box);                //ThuApr 12 2007 00:00:00 GMT+0800 //直接放日期的毫秒數也是可以的。var box = newDate(1176307200000);                alert(box); //預設自動後台調用Data.parse()var box = newDate('4/12/2007');                        alert(box); //沒有傳入日期格式參數,返回NaNalert(Date.parse());                 //Firefox返回Invalid Date(無效的日期)//Google返回一個混亂的日期//IE返回一個NaNvar box = newDate('msdfkl21123');        //亂寫的日期格式,會返回InvalidDate(無效的日期)alert(box); var box = new Date('May25,2007');                //第二種寫法alert(box); var box = new Date('ThuApr 12 2007 22:22:22GMT+0800');        //第三種寫法alert(box); //1月沒有32號,只有31號,多出來的,可以進到2月//Firefox2月1號                        向後增加+1//GoogleInvalid Date(無效的日期)//IE2月1號//Opera是1月30號   向前減少-1var box = newDate('January 32, 2007');alert(box); //必須傳入年份和月份,不傳,就各種錯誤,負值,有NaNalert(Date.UTC());                 //返回毫秒數alert(Date.UTC(2007,10));         //UTC世界協調時間,他會按照他的地區基準來計算var box = newDate(Date.UTC(2007, 10, 15, 17, 22, 45, 15));alert(box); //如果不加Date.UTC,那麼就會返回本地的時間var box = new Date(2007,10, 15, 17, 22, 45, 15);alert(box); //月份要加1才是最終的月份alert(box.getMonth() +1); //東八區有8個小時的差距alert(box.getUTCHours());所有getUTCHous和getHous相差8個小時 var box = new Date(2007,10, 15, 17, 22, 45, 15);alert(box);                                                        //ThuNov 15 2007 17:22:45 GMT+0800alert('toString:' +box.toString());//Thu Nov 15 2007 17:22:45 GMT+0800alert('toLocaleString:' +box.toLocaleString());//2007-11-15 17:22:45 PSGoogle返回的Thu Nov 15 2007 17:22:45GMT+0800alert('valueOf:' +box.valueOf());//1195118565015

總結

String對象為使用字串提供了大量便捷的方法,如計算字串的長度,在字串中尋找一個自串,選擇字串中的一個子串等.

Math對象一個由javascript執行環境自動建立的對象,它提供了大量數學運算的方法和屬性.

Number對象各種進位的數之間的轉換.

Array對象提供了運算元組的方法和屬性.例如,獲得數組的長度,對數組示範進行排序,合并兩個數組等.

Date對象提供了儲存日期,計算日期,獲得日期和時間的相關方法.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.