您知道,slice()返回切片的项目。例如,“ hello” .slice(1,3)返回“ ell”,而不返回其余的。我很好奇的是:是否有任何函数或方法来获取slice()的其余部分?
答案 0 :(得分:4)
String.prototype.remainderOfSlice = function(begin, end) {
begin = begin || 0
end = (end === undefined) ? this.length : end
if (this.slice(begin, end) === '') return this + ''
return this.slice(0, begin) + this.slice(end)
}
console.log("hello".slice()) // "hello"
console.log("hello".remainderOfSlice()) // ""
console.log("hello".slice(-3)) // "llo"
console.log("hello".remainderOfSlice(-3)) // "he"
console.log("hello".slice(-3, 0)) // ""
console.log("hello".remainderOfSlice(-3, 0)) // "hello"
console.log("hello".slice(1, 3)) // "el"
console.log("hello".remainderOfSlice(1, 3)) // "hlo"