charAt(index):回傳指定索引位置的字符。slice(start, end) 與 substring(start, end):都取起始位置到結束位置之間的字串,不含結束位置。省略第二參數時預設到字串結尾。兩者的主要差別在負數處理:slice() 的負數從尾端往前計算,substring() 則把負數當成 0,並自動把較大的值放到後面。例如 substring(2, -3) 等同 substring(0, 2)。
substr(start, length):從 start 開始取 length 個字符。indexOf(str, fromIndex):從前往後搜尋第一次出現的索引,找不到回傳 -1。lastIndexOf(str, fromIndex):從後往前搜尋。split(separator, howmany):以分隔符切成陣列;傳入空字串可把字串拆成單字。parseInt(string, radix) 解析字串並回傳整數,radix 指定進位制,預設十進制;parseFloat(string) 則回傳浮點數。
var a1 = new Array();
var a2 = new Array(size);
var a3 = new Array(0, 1, 2);
var a4 = [0, 1, 2];
join(separator):把每個元素轉為字串後,以 separator 串接成一個字串。reverse():就地反轉元素順序,無回傳值。sort():就地排序,無回傳值。function functionName(arg0, arg1) {
statements
return expression;
}
參數可透過 arguments 物件存取,例如 arguments[0] 是傳入的第一個值。Math.random() 則回傳 0 到 1 之間的隨機數。
包含 window、document、location、navigator、screen 五個核心物件。
Window 常用方法
window.moveBy(dx, dy) // 相對目前位置移動
window.moveTo(x, y) // 移動到絕對座標
window.resizeBy(dw, dh) // 相對調整大小
window.resizeTo(w, h) // 調整為指定大小
window.open(url, name, features)
window.close()
window.alert("...") / window.confirm("...") / window.prompt(text, defaultText)
window.history.back() / window.history.forward() / window.history.go(-1)
window.status = "..." // 設定狀態列文字
其他物件
window.document,最常用的是 document.write()。window.location,常用屬性 location.href = URL。navigator.userAgent 是判斷瀏覽器的傳統依據。availHeight、availWidth、colorDepth、height、width。