var person=new Object( ); person.name="webb"; person.age=25;
var person={
name:"webb",
age:25
};
var colors=new Array( );
var colors=new Array(20); //也可设置length属性
var colors=new Array("red","blue","green"); //包含3个字符串的数组
var colors=Array(3); //可省略new操作符
var colors=["red","blue","green"];
alert(colors[0]); //显示第一项
colors[2]="black"; //修改第三项
colors[3]="brown"; //新增第四项
var colors=["red","blue","green"];
colors.length=2;
alert(colors[2]); //undefined
colors[colors.length]="black"; //在末尾添加项
if(value instanceof Array){
//对数组执行某些操作
}
if(Array.isArray(value)){
//对数组执行某些操作
}
var colors=["red","blue","green"];
alert(colors.toString()); //red,blue,green
alert(colors.valueOf()); //red,blue,green
alert(colors); //red,blue,green
var person1={
toLocaleString:function(){
return "webbxx";
},
toString:function(){
return "webb";
}
};
var person2={
toLocaleString:function(){
return "susanxx";
},
toString:function(){
return "susan";
}
};
var people=[person1,person2];
alert(people); //webb,susan
alert(people.toString()); //webb,susan
alert(people.toLocaleString()); //webbxx,susanxx
使用join( )方法也可输出数组,并可指定分隔符,默认为逗号:
var colors=["red","blue","green"];
alert(colors.join(",")); //red,blue,green
alert(colors.join("||")); //red||blue||green
var colors=new Array();
var count=colors.push("red","green");
alert(count); //2
count=colors.push("black");
alert(count); //3
var item=colors.pop();
alert(item); //"black"
alert(colors.length); //2
var values=[0,1,5,10,15];
values.sort();
alert(values); //0,1,10,15,5
function compare(value1,value2){
if(value1<value2){
return -1; //value1在value2之前
}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
var colors=["red","blue","green"];
var colors2=colors.concat("yellow",["black","brown"]);
alert(colors); //red,blue,green
alert(colors2); //red,blue,green,yellow,black,brown
var colors=["red","green","blue","yellow","purple"];
var colors2=colors.slice(1); //green,blue,yellow,purple
var colors3=colors.slice(1,3); //green,blue,yellow
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
var someResult=numbers.every(function(item,index,array){
return item>2;
});
alert(someResult); //true
var filterResult=numbers.every(function(item,index,array){
return item>2;
});
alert(filterResult); //[3,4,5,4,3]
var mapResult=numbers.every(function(item,index,array){
return item*2;
});
alert(mapResult); //[2,4,6,8,10,8,6,4,2]
var values=[1,2,3,4,5];
var sum=values.reduce(function(prev,cur,index,array){
return prev+cur;
});
alert(sum); //15
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有