1、slice没有参数,拷贝一个数组,跟原数组无关联
var arr1 = ['a', 'b', 'c'];
var arr2 = arr1.slice();
console.log(arr2,arr1==arr2);
输出:['a', 'b', 'c'] false
2、slice(start,end)有参数
从start下标开始(包括start),到end(不包括end,如果没有end参数,则到字符串的结尾),复制数组到新的数组
var arr1 = ['a', 'b', 'c', 'e', 'f'];
var arr2 = arr1.slice(2);
console.log(arr2);
输出:['c', 'e', 'f']
var arr3 = arr1.slice(1,3);
console.log(arr3);
输出:['b', 'c']
3、slice参数是负数的,-1从最后一位开始,-2从倒数第二位开始
var arr1 = ['a', 'b', 'c', 'e', 'f'];
var arr2 = arr1.slice(-2,-1);
console.log(arr2);
输出:["e"]
4、arr.slice(NaN) arr.slice(null) NaN null这两个是无效参数,默认当0
var arr = ['a', 'b', 'c', 'e', 'f'];
var arr1 = arr.slice(NaN) ;
var arr2 = arr.slice(null) ;
console.log(arr1,arr2);
输出:['a', 'b', 'c', 'e', 'f']['a', 'b', 'c', 'e', 'f']
5、arr.slice(undefined) undefined在javascript中当成空参数,即等同没有传参数
var arr = ['a', 'b', 'c', 'e', 'f'];
var arr1 = arr.slice(undefined,undefined) ;
console.log(arr1);
输出:['a', 'b', 'c', 'e', 'f']
6、arr.slice('1') arr.slice([1]) 中的参数会被转化成数字
var arr = [1,2,3,4,5];
console.log(arr.slice('1'));
console.log(arr.slice([1]));
输出:
[2, 3, 4, 5]
[2, 3, 4, 5]