引用类型
- Object类型
- Array类型
- Date类型
- RegExp类型
- Function类型
- 基本包装类型
- 单体内置对象
1.Object
var person = {}
person.name = "Nicholas";
person.age = 29;
alert(person["name"]); //"Nicholas"--优点是可以通过变量来访问属性
alert(person.name); //"Nicholas"
2.Array
.length:
数组的项数,length不是只读的,可以从数组的末尾移项或向数组中
删除新项:var colors = ["red", "blue", "green"]; colors.length = 2; alert(colors[2]); //undefined "green"删除
添加新项:
var colors = ["red", "blue", "green"]; colors[colors.length] = "black"; //在位置3增加black colors[colors.length] = "brown"; //在位置4增加brown
instanceof:
确定某个对象是不是数组,
用法:
if (value instanceof Array){
}
但是如果网页有两个以上不同版本的Array不好区分。.isArray()
最终确定某个值是不是数组,
用法:
if (Array.isArray(value)){
}数组转字符串.toString
var colors = ["red", "blue", "green"]; alert(colors.toString()); //red,blue,green alert(colors.valueof()); //red,blue,green alert(colors); //red,blue,green--alert()要接收字符串参数,会在后台调用toString()方法 alert(colors.join(",")); //red,blue,green alert(colors.join("||")); //red||blue||green
栈方法 .push() .pop()
LIFO:后进先出的数据结构
push():添加到数组末尾,返回修改后的数组长度
pop():从数组末尾移除一项,减少数组的length,返回移除的项var colors = new Array(); var count = colors.push("red", "green"); alert(count); //2 var item = colors.pop(); alert(item); //"green"
队列 .push() .shift() .unshift()
FIFO:先进先出的数据结构
push():数组末端添加项
shift():移除数组中第一项并返回该项
unshift():在数组前端添加任意项并返回新数组的长度var colors = new Array(); var count = colors.push("red", "green"); alert(count); //2 count = colors.push("black"); alert(count); //3 var item = colors.shift(); //取得第一项 alert(item); //"red" alert(colors.length); //2
重排序方法 .sort() .reverse()
reverse():反转数组的顺序
sort():按从小到大排序,但是会受字符串比较影响function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } } var values = [0, 1, 5, 10, 15]; values.sort(compare); alert(values); //0,1,5,10,15
操作方法 .concat() .slice() .splice()
concat():从末尾拼接数组,返回新构建的数组
slice():从指定位置到结束位置之间(不包括结束位置)的项,不影响原数组
splice():可以删除、插入、替换数组var colors = ["red", "green"]; var colors2 = colors.concat("yellow", "black"); alert(colors2); // red, green, yellow, black ---------- var colors = ["red", "green", "blue", "yellow", "purple"]; var colors2 = colors.slice(1); //green, blue, yellow, purple var colors3 = colors.slice(1,4); //green, blue, yellow ---------- var colors = ["red", "green", "blue"]; var removed = colors.splice(0,1); //删除第一项 alert(colors); //green, blue alert(removed); //red removed = colors.splice(1, 0, "yellow", "orange"); //从位置1开始插入两项 alert(colors); //green, yelloe, orange, blue alert(removed); //返回空数组
位置方法 .indexOf() .lastIndexOf()
indexOf(): 从数组开头向后查到项在数组中的位置,没有返回-1迭代方法 .every() .some() .filter() .forEach() .map()
every(): 查数组中的每一项是否满足条件 true or false
some(): 查数组中的是否有一项满足条件 true or false
filter(): 筛选满足函数条件的数组 list
map(): 数组中的每一项在对应的位置调函数 list
forEach(): 数组中的每一项运行传入的函数 没有返回值 本质与for循环一样归并方法 .reduce() .reduceRight()
reduce(): 从数组第一项开始,逐个遍历到最后
reduceRight(): 从数组最后一项开始,向前遍历到第一项
四个参数:前一个值,当前值,项的索引,数组对象。函数返回的值作为第一个参数传递给下一项。var values = [1,2,3,4,5] var sum = values.reduce(function(prev, cur, index, array){ return prev + cur; }); alert(sum); //15