《javascript進階程式設計》學習筆記(五):javascript 的本機物件

來源:互聯網
上載者:User
 


(1)Array:

方法1:
var aValue = new Array(20);
方法2:
var aValue = new Array();
aValue[0] = "a";
aValue[1] = "b";
...
方法3:
var aValue = new Array("a","b",...)
方法4:
var aValue = ["a","b",...](無需明聲明使用Array類)

aValue.join(參數)方法,把數組各項串連起來,串連符是參數。

aValue = new Array("a","b","c")
aValue.join("-")  //輸出 :"a-b-c"

一個逆向的方法是:String.split(參數)方法。把string類型轉換為Array
var sColor = "green";
var aColor = sColor.split("");//得到數組,["g","r","e","e","n"];

Array類的concat()和slice()方法同string類

Array類的push(),pop()方法和unshift(),shift(),方法,分別提供了其類似棧(後進先出LIFO)的行為和類似隊列(先進先出LILO)的行為。

Array_name.reverse() 顛倒數組順序
Array_name.sort()  給數組排序
Array_name.splice()把資料項目插入數組的中部(十分有用且有趣。)

(2)Date:

比較常用的:
var _date = new Date();
var _year = _date.getFullYear(); 當前年
var _month = _date.getMonth();  當前月
var _day = _date.getDate();  當前日

alert(_date.toTimeString().split(" ")[0]); 當前時分秒

(3)Global  內建對象

比較特殊,比如isNan(),parseInt()等,都是它的函數。

encodeURI(),encodeURIComponent(),decodeURI() 和decodeURIComponet() 與BOM方法中的escape()和unescape()最大的區別就是,escape()對於非ASCII字元集的符號不編碼,所以,提倡用 encodeURI()來編碼

(4)Math 內建對象

min()
max()
abs()絕對值,
ceil()向上舍入
floor()向下舍入
round()四捨五入
alert(Math.ceil(25.5)); //output 26
alert(Math.floor(25.5));//output 25
alert(Math.round(25.5));//output 26
random() 返回一個0到1之間的隨機數
例如:選擇從1到10的隨機數:
var iNum = Math.floor(Math.random()*10 + 1)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.