引用类型是一种将数据和功能组合到一起的数据结构,它与类相似,但是是不同的概念:ECMAScript虽然是一门面向对象的语言,但它不具备传统的面向对象语言所支持的类和结构等基本结构。引用类型也被称为“对象定义”。
一、Object类型
创建实例方式:
1使用new操作符+构造函数
var person = new Object(); person.name = "Lillian"; person.age = 29;
2使用对象字面量表示法
2.1属性不加引号
var person = { person: "Lillian", age:29 };
2.2属性加引号
var person = { "person": "Lillian", "age":29, 5:true //数值属性会自动转化成字符串 };
2.3使用空花括号
var person = {}; person.name = "Lillian"; person.age = 29;
访问属性的方式:
使用点或方括号:
alert(person.name); alert(person[age]);
二、Array类型
创建方式:
1使用Array构造函数
var colors = new Array(); var colors = new Array(20); //length = 20 var colors = new Array("red","blue","yellow"); //length = 3
2使用数组字面量
var colors = ["red", "blue", "yellow"]; var names = []; var values = [1,2,];//不要这样,会创建一个包含2项或3项的数组 var options = [,,,,,];//不要这样,会创建一个包含5项或6项的数组
除了Object类型,Array类型恐怕是JavaScript最常见的类型了,它的方法很多,这里先通过导图一览,再依次举例。
修改Array数组的内容:
var colors = ["red", "blue", "yellow"]; colors[0] = "green"; //修改第一项 colors[colors.length] = "black";//增加一项,值为"black" colors.length = 5; alert(colors[4]);//长度增加了1,但是未赋值,这一项为undefined colors.length = 1; alert(colors[1]);//undefined, 通过设置长度为1,移除了除第一项外的所有项
检测某个对象是不是数组:
if (value instanceof Array) {};//不适用于包含多个框架的网易 if( Array.isArray(value)){}; //对浏览器版本要求比较高
转换方法:
var colors = ["red", "blue", "yellow"]; colors.toString(); //"red", "blue", "yellow" colors.toLocaleString(); //"red", "blue", "yellow" colors.valueOf();//["red", "blue", "yellow"] alert(colors);//"red", "blue", "yellow" alert(colors.join(","));//"red", "blue", "yellow" alert(colors.join("||"));//"1||2||3"
栈方法:LIFO
var colors = ["red", "blue", "yellow"]; colors.push("black"); colors.pop();//“black"
队列方法:LILO
var colors = ["red", "blue", "yellow"]; colors.push("black"); colors.shift();//“red"
重排序方法:
var values = [1,0,15,14,2]; values.reverse(); //[2, 14, 15, 0, 1] values.sort();//[0, 1, 14, 15, 2] function compare(value1, value2){ if(value1 < value2){ return -1; }else if(value1 > value2){ return 1; }else{ return 0; } } values.sort(compare);// 0,1,2,14,15
操作方法:
//concat()将接收到的参数添加在末尾 var colors = ["red","green","blue"]; var colors2 = colors.concat("yellow",["black", "brown"]); //colors2:"red","green","blue","yellow",black", "brown" //slice()在当前数组中截取部分 var colors = ["red","green","blue","yellow"]; var colors2 = colors.slice(1);//["green","blue","yellow"] var colors3 = colors.slice(1,3);//["green","blue"] //splice()删除和插入 var colors = ["red","green","blue","yellow"]; var removed = colors.splice(0,2); //从第一项开始,删除两项 var colors = ["red","green","blue","yellow"]; var removed = colors.splice(1,0,"black"); //在第二项后,插入一项 var colors = ["red","green","blue","yellow"]; var removed = colors.splice(1,1,"black"); //在第二项后,删除一项后,再插入一项
位置方法:
var numbers = [1,2,3,4,5,4,3,2,1]; alert(numbers.indexOf(4)); alert(numbers.indexOf(4,4)); alert(numbers.lastIndexOf(4));
迭代方法:
/* 迭代方法:函数(数组项的值,该项在数组中的位置,数组对象本身) */ //every():如果每一项都返回true,则函数返回true var numbers = [1,2,3,4,5,4,3,2,1]; var result = numbers.every(function(item, index, array){ return (item > 2); }); alert(result); //false // some():如果有一项返回true,则函数返回true var numbers = [1,2,3,4,5,4,3,2,1]; var result = numbers.some(function(item, index, array){ return (item > 2); }); alert(result); //true //filter():过滤出满足条件的值 var numbers = [1,2,3,4,5,4,3,2,1]; var result = numbers.filter(function(item, index, array){ return (item > 2); }); alert(result); //[3,4,5,4,3] //map():返回函数调用的结果并组成数组 var numbers = [1,2,3,4,5,4,3,2,1]; var result = numbers.map(function(item, index, array){ return (item * 2); }); alert(result); //[2,3,6,8,10,8,6,4,2]; //forEach():没有返回值 var numbers = [1,2,3,4,5,4,3,2,1]; numbers.forEach(function(item, index, array){ alert(item); });
缩小方法:
/* 迭代函数:(前一个值,当前值,项的索引,数组对象) 迭代所有项,返回一个最终结果 reduceRight是从数组的最后一项向前遍历 */ var values = [1,2,3,4,5]; var sum = values.reduce(function(prev, cur, index, array){ //var sum = values.reduceRight(function(prev, cur, index, array){ return prev + cur; }); alert(sum);