/ 字面量方式:
// 这个方法也是我们最常用的,在初始化数组的时候 相当方便
var
a = [3, 11, 8];
// [3,11,8];
// 构造器:
// 实际上 new Array === Array,加不加new 一点影响都没有。
var
a = Array();
// []
var
a = Array(3);
// 一个参数,代表数组长度,值都没有定义,[undefined,undefined,undefined]
var
a = Array(3,11,8);
//多个参数,代表插入数组的值 [ 3,11,8 ]
var mycars=new Array() mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW"
var mycars=new Array(3) mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW"
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" var arr3 = new Array(2) arr3[0] = "William" arr3[1] = "Franklin" document.write(arr.concat(arr2,arr3)
) </script>
join() 方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.join()
) </script>
George,John,Thomas
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join(".")
)
</script>
George.John.Thomas
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
如果 end 未被规定,那么 slice() 方法会选取从 start 到数组结尾的所有元素。
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.slice(1)
+ "<br />")
document.write(arr)
</script>
George,John,Thomas John,Thomas George,John,Thomas
<script type="text/javascript">
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
document.write(arr.slice(2,4)
+ "<br />")
document.write(arr)
</script>
George,John,Thomas,James,Adrew,Martin Thomas,James George,John,Thomas,James,Adrew,Martin
begin从0开始计数,不包括end
<script type="text/javascript">
function sortNumber(a,b)
{
return a - b
}
var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"
document.write(arr + "<br />")
document.write(arr.sort(sortNumber)
)
</script>
10,5,40,25,1000,1 1,5,10,25,40,1000
return a-b 跟这个一样:
if(a<b){ return -1; } if(a>b){ return 1; } return 0;
原文:https://www.cnblogs.com/cowboybusy/p/11096629.html