JavaScript的引用类型1(Object Array)

来源:互联网 发布:淘宝客服物流用语 编辑:程序博客网 时间:2024/05/21 10:46

Object类型 Array类型 Date类型 RegExp类型 Function类型

引用类型的值也就是对象是引用类型的一个实例,引用类型是一种数据结构,将数据和功能组合在一起。

//新对象是使用new操作符后跟一个构造函数创建的var person=new Object();//创建一个Object引用类型的一个新实例,使用的构造函数是Object()

1.Object 类型

使用最多的类型

  • 创建方式
//推荐使用对象字面量表达法var person= {            name: "Curry",            age: 30//最后一个属性不加逗号        };//new操作符var person1=new Object();person1.name="Curry";person1.age=30;
  • 访问对象的属性
alert(person["name"]);alert(person.name);//没有特殊要求推荐使用点表示法

2.Array类型

使用次数其次的类型

1.基础知识
  • 创建
//1.new操作符+Array()构造函数(new可省略)var colors=new Array();var colors=new Array(20);//创建length值为20var colors=new Array("red","blue","green");//创建包含3个字符串的数组var colors=Array(3);//省略new
//2.数组字面量表示法var colors=["red","blue","green"];//创建包含3个字符串的数组var color=[];//创建空数组
  • 读取和设置数组的值
var colors=["red","blue","green"];alert(colors[0]);//访问第一项colors[1]="black";//修改第二项colors[3]="white";//新增第四项
  • 通过length从数组末尾移除或添加
var colors=["red","blue","green"];colors.length=2;//长度为2 也就是删除第3项alert(colors[2]);//undefinedvar colors=["red","blue","green"];colors.length=4;//增加第四项alert(colors[3]);//undefined,新增的第四项值为undefinedvar colors=["red","blue","green"];colors[colors.length]="brown";//在位置4添加"black"
2.检测数组

Array.isArray()方法

if(Array.isArray(value)){    //对数组执行某些操作}
3.转换方法

toString() valueOf() toLocaleString()

toString():返回数组中的每个值的字符串形式拼而成的一个以逗号分隔的字符串
valueof():返回的还是数组

var colors=["red","blue","green"];alert(colors.toString());//red,blue,greenalert(colors.valueOf());//red,blue,greenalert(colors);//red,blue,green
3.重排序

reverse() sort()

var values=[0,1,5,10,15];values.reversea();alert(values);//15,10,5,1,0进行反转var values=[0,1,5,10,15];values.sort();alert(values);//0,1,10,15,5 升序排列//但是进行字符串比较是"5"排最后
//修正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
4.操作方法

concat( )、 slice( ) 、splice( )

//concat()方法可以基于当前所有数组项创建新的数组var colors=["red","blue","green"];var colors2=colors.concat("yellow",["black","brow"]);alert(colors);//red,blue,greenalert(colors2);//red,blue,green,yellow,black,brown
//slice()基于当前数组中的一个或多个创建新数组,可以接受一个或两个参数var colors=["red","blue","green","yellow","purple"];var color2=colors.slice(1);//从1到结尾var color3=colors.slice(1,4);//从1到3 不包含4alert(colors);//blue,green,yellow,purplealert(colors2);//red,blue,green
//splice()删除、插入和替换var colors=["red","green","blue"];var removed=colors.splice(0,1);//0起始位置,1要删除的项数。即删除第一项alert(colors);//green,bluevar removed=colors.splice(1,0,"yellow","orange");//起始位置、删除项数和要插入的项
5.位置方法

indexOf() 、lastIndexOf()

indexOf():可以接收两个参数:要查找的项和表示要查找的其实位置的索引(可选),从数组的开头开始
lastIndexOf():从数组的末尾开始
返回:在数组中的位置

var numbers=[1,2,3,4,5,6,5,4,3,2];alert(numbers.indexOf(4));//3
6.迭代方法

some( )、every( )、filter( )

var numbers=[1,2,3,4,5,4,3,2,1];var everyResult=numbers.every(function(item,index,array){    return (item>2);});alert(everyResult);//false
7.归并

reduce( ) reduceRight( )

var numbers=[1,2,3,4,5]var sum=numbers.reduce(function(prev,cur,index,array){    return (prev+cur);});alert(sum);//15
阅读全文
0 0
原创粉丝点击