专注于技术经验交流

水至清则无鱼、宁静而致远!

技术、经验、学习共同打造网络新生活!
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JavaScript学习与实践(3)

Posted on 2007-02-01 17:03  小鱼儿  阅读(178)  评论(0编辑  收藏  举报

JS中的数组对象,他用于使用一个变量名字存储一组值,

        例子:这个例子是创建一个数组,并分配了数值,最后打印出其中的值,

        

<html>
<body>
<script type="text/javascript">
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br />")
}
</script>
</body>
</html>
下面是一个for...in,怎么用他来循环便利数组中的每个元素呢?
<html>
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>
下面是一个连接两个数组的方法concat()
<html>
<body>
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Tove"
arr[2] = "Hege"
var arr2 = new Array(3)
arr2[0] = "John"
arr2[1] = "Andy"
arr2[2] = "Wendy"
document.write(arr.concat(arr2))
</script>
</body>
</html>
 
下面是一个join()方法,他是把一个数组中所有的元素方到一个字符串中,,其中可以指定标点符号例子:
 
<html>
<body>
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr.join() + "<br />")
document.write(arr.join("."))
</script>
</body>
</html>
下面一个是分类,区别的sort(),他用于字面的,或者是数字的下面有两个例子来加以区别:
 
<html>
<body>
<script type="text/javascript">
var arr = new Array(6)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
arr[3] = "Kai Jim"
arr[4] = "Borge"
arr[5] = "Tove"
document.write(arr + "<br />")
document.write(arr.sort())
</script>
</body>
</html>
 
第二个

<html>
<body>
<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>
</body>
</html>
上面这个我还没有理解,希望大家,指导我一下,谢谢
 

Array Object Methods

FF: Firefox, N: Netscape, IE: Internet Explorer

Method Description FF N IE
concat() Joins two or more arrays and returns the result 1 4 4
join() Puts all the elements of an array into a string. The elements are separated by a specified delimiter 1 3 4
pop() Removes and returns the last element of an array 1 4 5.5
push() Adds one or more elements to the end of an array and returns the new length 1 4 5.5
reverse() Reverses the order of the elements in an array 1 3 4
shift() Removes and returns the first element of an array 1 4 5.5
slice() Returns selected elements from an existing array 1 4 4
sort() Sorts the elements of an array 1 3 4
splice() Removes and adds new elements to an array 1 4 5.5
toSource() Represents the source code of an object 1 4 -
toString() Converts an array to a string and returns the result 1 3 4
unshift() Adds one or more elements to the beginning of an array and returns the new length 1 4 6
valueOf() Returns the primitive value of an Array object 1 2 4


Array Object Properties

Property Description FF N IE 
constructor A reference to the function that created the object 1 2 4
index   1 3 4
input   1 3 4
length Sets or returns the number of elements in an array 1 2 4
prototype Allows you to add properties and methods to the object 1 2 4

New Document