| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 328 人关注过本帖
标题:为什么一加这段就undefined呢?
收藏  订阅  推荐  打印
baby69yy2000
Rank: 2
等级:注册会员
帖子:67
积分:790
注册:2007-4-5
为什么一加这段就undefined呢?

就是模仿java里面的ArrayList。
为什么一加这段就undefined呢?
ArrayList.prototype.ensureCapacity = function(iCapacity) {
this.arr = new Array(iCapacity);
};
复制内容到剪贴板
代码:
[code=HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Person</title>
        <script language="JavaScript">
            function ArrayList() {
                    this.arr = new Array();
                    this.iListSize = 0;
                    
                ArrayList.prototype.ensureCapacity = function(iCapacity) {
                    this.arr = new Array(iCapacity);
                };
               
                ArrayList.prototype.add = function(item) {
                    this.arr.push(item);
                    this.iListSize++;
                };
               
                ArrayList.prototype.get = function(index) {
                    return this.arr[index];
                };
               
                ArrayList.prototype.size = function() {
                    return this.iListSize;
                };
            }
            
            var aL = new ArrayList();
            aL.ensureCapacity(3);
            aL.add("A");
            aL.add("B");
            aL.add("C");
            for (var i = 0; i < aL.size(); i++) {
                alert(aL.get(i));
            }
        </script>
    </head>
    <body>
    </body>
</html>
2008-5-26 06:37
渚薰
Rank: 12Rank: 12Rank: 12
等级:贵宾
帖子:1132
积分:12338
威望:22
注册:2006-8-6

function ArrayList() {
                    this.arr = new Array();
                    this.iListSize = 0;
            }

               ArrayList.prototype.ensureCapacity = function(iCapacity) {
                    this.arr = new Array(iCapacity);
                };
               
                ArrayList.prototype.add = function(item) {
                    this.arr.push(item);
                    this.iListSize++;
                };
               
                ArrayList.prototype.get = function(index) {
                    return this.arr[index];
                };
               
                ArrayList.prototype.size = function() {
                    return this.iListSize;
                };

            
            var aL = new ArrayList();
            aL.ensureCapacity(3);
            aL.add("A");
            aL.add("B");
            aL.add("C");
            for (var i = 0; i < aL.size(); i++) {
                alert(aL.get(i));
            }

个人ajax技术专题站: http://www.ajaxgo.org 我不会闲你烦,只会闲你不够烦!
2008-5-26 10:14
baby69yy2000
Rank: 2
等级:注册会员
帖子:67
积分:790
注册:2007-4-5

先谢斑竹了!!
啊,是push方法弄错了呀。
这样先分配空间等于没分一样呀
复制内容到剪贴板
代码:
<script language="JavaScript">
            function ArrayList() {
                    this.arr = new Array();
                    this.iListSize = 0;
            }

               ArrayList.prototype.ensureCapacity = function(iCapacity) {
                    this.arr = new Array(iCapacity);
                    alert(this.arr.length); // 3
                };
               
                ArrayList.prototype.add = function(item) {
                    this.arr.push(item);
                    this.iListSize++;
                };
               
                ArrayList.prototype.get = function(index) {
                    return this.arr[index];
                };
               
                ArrayList.prototype.size = function() {
                    return this.iListSize;
                };

            
            var aL = new ArrayList();
            aL.ensureCapacity(3);
            aL.add("A");
            aL.add("B");
            aL.add("C");
            alert(aL.get(3)); // A
            alert(aL.get(4)); // B
            alert(aL.get(5)); // C
            /*for (var i = 0; i < aL.size(); i++) {
                alert(aL.get(i));
            }*/
        </script>
[ 本帖最后由 baby69yy2000 于 2008-5-26 11:48 编辑 ]

2008-5-26 11:44
共有 327 人关注过本帖
发新话题
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.050273 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved