5 個JavaScript 中 Slice()的用例

y = [1, 2, 3, 4, 5, 6]y.slice(2, -2) // will return [3, 4]
const midtermGrades = updatedGrades.slice();
const successfulStudents = allStudents.slice(10);
const unsatisfactoryStudents = allStudents.slice(-10);
function TransformToArray(){return Array.prototype.slice.call(arguments);}var newArray = TransformToArray("1", "2", "3", "4");console.log(newArray); // ["1", "2", "3", "4"];
var p = document.querySelectorAll(‘p’);var pNodes = Array.prototype.slice.call(p);
String.prototype.append = function (index,value) {return this.slice(0,index) + value + this.slice(index);};var s = "Happy year";alert(s.append(6,"new "));
復(fù)制數(shù)組
構(gòu)造一個從 n 開始的子數(shù)組
將類數(shù)組對象轉(zhuǎn)換為數(shù)組
將 NodeList 轉(zhuǎn)換為數(shù)組
替換字符串中的特定索引
學(xué)習(xí)更多技能
請點擊下方公眾號 

評論
圖片
表情
