当接收的参数是负数时,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)+'
');