<分区>
如您所知,slice() 返回切片项。例如,"hello".slice(1, 3) 返回“ell”,而不是余数。我很好奇的是:是否有任何函数或方法来获取 slice() 的剩余部分?
标签 javascript slice
<分区>
如您所知,slice() 返回切片项。例如,"hello".slice(1, 3) 返回“ell”,而不是余数。我很好奇的是:是否有任何函数或方法来获取 slice() 的剩余部分?
最佳答案
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"
关于javascript - 有没有Slice()的反函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52398284/