object类型:
创建对象:
var person=new Object();
person.name="Nike";
person.age=29;
另一种方法:
var person={
name:'Nike',
age:29
};
Array类型:
创建数组:
var colors=new Array();
var colors=new Array(20);//数组长度为20
var colors=new Array("red","blue","green");
var colors=['red','blue','green'];
var names=[];//空数组
数组的length属性:通过设置这个属性,可以从数组的末尾移除项或向数组中添加新项。
var colors=['red','blue','green'];
colors.length=2;
document.writeln(colors);//red,blue
var colors=['red','blue','green'];
colors.length=4;
document.writeln(colors[3]);//undefined
检测数组:
var colors=['red','blue','green'];
var items=['1','2','3'];
if(colors instanceof Array){
alert("colors is Array");//colors is Array
}
if(Array.isArray(colors)){
alert("items is Array");//items is Array
}
转换方法:toLocalString(),toString(),valueOf()
var colors=['red','blue','green'];
document.write(colors.toString());//red,blue,green
document.write("<br>");
document.write(colors.valueOf());//red,blue,green
document.write("<br>");
document.write(colors);//red,blue,green
采用document.write("<br>");为了转行更清楚看效果。
如果使用join()方法,则可以使用不同的分隔符来构建这个字符串。
var colors=['red','blue','green'];
document.write(colors.join(','));//red,blue,green
document.write("<br>");
document.write(colors.join('|'));//red|blue|green
栈方法:后进先出
push()方法接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度。
pop()方法则从数组末尾移除最后一项,减少数组的length值,然后返回移除的项。
var colors=new Array();
var count=colors.push("red","green");
document.write(count);//2
document.write("<br>");
count=colors.push("black");
document.write(count);//3
var item=colors.pop();
document.write(item);//black
document.write(colors.length);//2
var colors=['red','blue'];
colors.push('brown');
colors[3]="black";
document.write(colors.length);//4
var item=colors.pop();
document.write(item);//black
队列方法:后进后出
push()是向数组末尾添加项的方法;
shift()是移除最前面的一项;
unshift()是在数组前端添加任意个项并返回新数组的长度。
var colors=new Array();
var count=colors.push("red","green");
document.write(count);//2
count=colors.push("black");
document.write(count);//3
var item=colors.shift();
document.write(item);//red
document.write("<br>");
document.write(colors.length);//2
var colors=new Array();
var count=colors.unshift("red","green");
document.write(count);//2
count=colors.unshift("black");
document.write(count);//3
var item=colors.pop();
document.write(item);//green
document.write(colors.length);//2
重排序方法:
reverse()方法会反转数组项的顺序。
sort()方法按升序排列数组项。
var values=[1,2,3,4,5];
values.reverse();
document.write(values);//5,4,3,2,1
document.write("<br>");
var values=[0,1,5,10,15];
values.sort();
document.write(values);//0,1,10,15,5 按字符串来比较
document.write("<br>");
function compare(value1,value2){
if(value1<value2){
return -1;
}else if(value1>value2){
return 1;
}else{
return 0;
}
}
values.sort(compare);
document.write(values);//0,1,5,10,15
操作方法:
concat()方法可以基于当前数组中的所有项创建一个新数组。该方法是拼接数组。
var colors=["red","green","blue"];
var colors2=colors.concat("yellow",["black","brown"]);
document.write(colors);//red,green,blue
document.write("<br>");
document.write(colors2);//red,green,blue,yellow,black,brown
slice()基于当前数组中的一或多个项创建一个新数组。
如果只有一个参数时,返回从该参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项但不包括结束位置的项。
var colors=["red","green","blue","yellow","purple"];
var colors2=colors.slice(1);
var colors3=colors.slice(1,4);
document.write(colors2);//green,blue,yellow,purple
document.write("<br>");
document.write(colors3);//green
splice()方法使用方式有三种:
删除:只需指定2个参数,要删除的第一项的位置和要删除的项数。
插入:只需提供3个参数,起始位置、0(删除项数)、要插入的项。
替换:只需指定3个参数,起始位置、要删除的项数、要插入的任意数量的项。
var colors=["red","green","blue"];
var removed=colors.splice(0,1);
document.write(colors);//green,blue
document.write("<br>");
var removed1=colors.splice(1,0,"yellow","orange");
document.write(colors);//green,yellow,orange,blue
document.write("<br>");
var removed2=colors.splice(1,1,"yellow1","orange1");
document.write(colors);//green,yellow1,orange1,orange,blue
位置方法:
indexOf()从数组的开头开始向后查找
lastIndexOf()从数组的末尾开始向前查找
var numbers=[1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4));//3
alert(numbers.lastIndexOf(4));//5
alert(numbers.indexOf(4,4));//5
alert(numbers.lastIndexOf(4,4));//3
迭代方法:
every() 对数组的每一项运行给定函数,如果该函数对每一项都返回true,则返回true;
filter() 对数组的每一项运行给定函数,返回该函数会返回true的项组成的数组;
foreach() 对数组的每一项运行给定函数,这个方法没有返回值;
map() 对数组的每一项运行给定函数,返回每次函数调用的结果组成的数组;
some() 对数组中的每一项给定函数,如果该函数对任一项返回true,则返回true。
var numbers=[1,2,3,4,5,4,3,2,1];
var everyResult=numbers.every(function(item,index,array){
return (item>2);
});
document.write(everyResult);//false
var someResult=numbers.some(function(item,index,array){
return (item>2);
});
document.write(someResult);//true
var numbers=[1,2,3,4,5,4,3,2,1];
var filterResult=numbers.filter(function(item,index,array){
return (item>2);
});
document.write(filterResult);//3,4,5,4,3
document.write("<br>");
var mapResult=numbers.map(function(item,index,array){
return (item*2);
});
document.write(mapResult);//2,4,6,8,10,8,6,4,2
归并方法:
reduce()从数组的第一项开始
reduceRight()从数组的最后一项开始
var values=[1,2,3,4,5];
var sum=values.reduceRight(function(prev,cur,index,array){
return prev+cur;
});
document.write(sum);//15
Date类型:
var now=new Date();
document.write(now);//Fri Aug 12 2016 21:47:44 GMT+0800 (中国标准时间)
var start=Date.now();
alert("loading...");//doSomething();
var stop=Date.now();
result=stop-start;
alert(result);