javascript当中Array对象用法

Array对象
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。


例 1.1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <script type="text/javascript">
        function sum(a)
        {
            var s = 0;
            for (var i = 0; i < a.length; i++)
            {
                s = s + a[i];
            }
            return s;
        }
        var a = [4,2,3];
        document.write(sum(a));

    </script>
</head>
<body>
1) do sum through array 马克-to-win
</body>
</html>

输出结果:

9 1) do sum through array 马克-to-win




例 1.2


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script>
    /*When the Global object is created, it always has at least the following properties:
       Object object       Function object       Array object       String object
       Boolean object       Number object       Date object       Math object
       Value properties
   */
        /*马克-to-winArray object is the same as the following statement. see the following reference, we can know  that first, we have array object, then [] delimiter feature is added in in the javascript 1.3,[]   也可以动态的加东西*/
        var a = [8,5,'29a'];
        document.write(typeof(a));
        document.write(typeof(a[1]));
        document.write(typeof(a[2]));
        a[3] = 'qixy';
        document.write(a[3]);
        /*the following two types only create empty array, its type is object. Also aa和aaa is like dynamic array and it can add different type of data such as string and number*/
        var aa = [];
        document.write(typeof(aa) + " is typeof(aa)");
        var aaa ;
        document.write(typeof(aaa));
        document.write(typeof(d));
        /*the following must be commented out, otherwise it report error.program stops here.*/
        //document.write(d);
        aaa = new Array();
        document.write(typeof(aaa) + " is typeof(aaa) after new Array");
        aa[0] = 'qixy';
        aa[2] = 8;
        aaa[1] = false;
        aaa[3] = null;
        d = new Array(4, 2, 6);
        document.write(a + "\n" + aa + "\n" + aaa + "\n" + d);
        /*aa[1] is undefined, while aaa[3] is null, */
        document.write("aa[1] is" + aa[1]);
        document.write("aaa[1] is" + aaa[1]);
        document.write("aaa[3] is" +aaa[3]);
    </script>
</head>
<body>
</body>
</html>


输出结果:

objectnumberstringqixyobject is typeof(aa)undefinedundefinedobject is typeof(aaa) after new Array8,5,29a,qixy qixy,,8 ,false,, 4,2,6aa[1] isundefinedaaa[1] isfalseaaa[3] isnull