1.可以用type of 来检测每个属性是否存在
2.数组的 length 属性不是只读的 ,因此通过这个属性 ,可以从数组的末尾移除项或向数组中添加新项
eg: 移除
var colors = ["red", "yellow", "grown"];
colors.length = 2;
alert(colors.length) // undefined
eg:新增
var colors = ["red", "yellow", "grown"]; colors[colors.length] = "#338bff"; alert(colors) //["red", "yellow", "grown", "#338bff"]
eg:特定位置新增
var colors = ["red", "yellow", "grown"]; colors[9] = "#338bff"; console.log(colors) //["red", "yellow", "grown", empty × 6, "#338bff"] console.log(colors.length ) // 10
3.sort 用法
eg:
var values = [10, 5, 1, 15 ,8]
function compare(val1, val2) {
if (val1 < val2) {
return -1 //如果降序这个取反
} else if (val1, val2) {
return 1
} else { // 如果降序这里取反
return 0
}
}
values.sort(compare)
document.write(values) // [1,5,8,10,15]