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 "));
复制数组
构造一个从 n 开始的子数组
将类数组对象转换为数组
将 NodeList 转换为数组
替换字符串中的特定索引
学习更多技能
请点击下方公众号
评论