當接收的參數是負數時,slice會將它字符串的長度與對應的負數相加,結果作為參數;substr則僅僅是將第一個參數與字符串長度相加后的結果作為第一個參數;substring則干脆將負參數都直接轉換為0。測試代碼如下:
var test = 'hello world';
document.write(test.slice(-3)+'
');???????? //rld
document.write(test.substring(-3)+'
');???? //hello world
document.write(test.substr(-3)+'
');??????? //rld
document.write(test.slice(3,-4)+'
');?????? //lo w
document.write(test.substring(1,0)+'
');?? //hel
document.write(test.substr(3,-4)+'
');