1.手搓数组反转
function reverse(arr){
if(arr instanceof Array){
var newArr = [];
for(var i = arr.length-1;i>=0;i--){
newArr[newArr.length] = arr[i];
}
return newArr;
}else{
alert('请传数组格式的值)
}
}
console.log(reverse([1,2,3,4,5,6,7]))
- instanceof 运算符,它可以用来检测是否为数组;
var arr = [];
var obj = {};
console.log(arr instanceof Array); // true
console.log(obj instanceof Array); // false
- isArray
var arr = [];
var obj = {};
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false