setCapture 與 releaseCapture 主要用於滑鼠事件捕獲。當某個元素呼叫 setCapture 後,即使滑鼠移出該元素,相關滑鼠事件仍可能繼續被它接收;releaseCapture 則用來釋放這種捕獲狀態。
document.getElementById("divMain").setCapture();
document.getElementById("divMain").releaseCapture();這組方法適合用於需要持續追蹤滑鼠拖曳或避免事件落空的情境。例如小範圍的拖曳物件,若沒有捕獲滑鼠,快速移動時可能會失去後續事件。
var isFreeze = true;
function change_capture(obj) {
isFreeze = !isFreeze;
if (isFreeze) {
obj.value = "releaseCapture";
document.getElementById("divMain").setCapture();
} else {
obj.value = "setCapture";
document.getElementById("divMain").releaseCapture();
}
}需要注意的是,setCapture 與 releaseCapture 主要作用於滑鼠事件,例如 mousedown、mouseup、mousemove、click 等,對鍵盤事件的效果有限。
在 JavaScript 物件導向應用中,call 與 apply 常用來改變函式內 this 的指向。兩者功能相似,差別在於參數傳遞方式:apply 接受陣列形式的參數,call 則是逐一列出參數。