3 回答

TA貢獻1777條經驗 獲得超3個贊
值得注意的是,當不是數組/字符串并且沒有方法時,使用Array.prototype.slice.call(arr, 0)
overarr.slice()
不會拋出錯誤。arr
arr
slice
也許他們想要一個更健壯的實現,因此使用第一種方法。

TA貢獻1862條經驗 獲得超7個贊
從簽名我可以說 list.slice()[0] 與 Array.prototype.slice.call(list, 0) 完全不同,在前面的語句中你首先調用 list.slice 并訪問它的第 0 個索引元素,在后面您正在調用 Array.slice 的語句將上下文作為列表的值傳遞,將 0 作為參數傳遞給切片函數。

TA貢獻1895條經驗 獲得超7個贊
在我看來,Array.prototype.slice.call支持 Array-Like Objects,比如 NodeList 集合。
const divs = document.querySelectorAll('div')
divs.slice(0)
//Uncaught TypeError: divs.slice is not a function
Array.prototype.slice.call(divs, 0)
//(222) [div.ng-toast.ng-toast--right.ng-toast--bottom.ng-toast--animate-fade, ……
添加回答
舉報