| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 772 人关注过本帖
标题:为什么一加这段就undefined呢?
只看楼主 加入收藏
baby69yy2000
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2007-4-5
收藏
 问题点数:0 回复次数:2 
为什么一加这段就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. <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>
搜索更多相关主题的帖子: undefined 
2008-05-26 06:37
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
收藏
得分:0 
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技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-05-26 10:14
baby69yy2000
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2007-4-5
收藏
得分:0 
先谢斑竹了!!
啊,是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>


[[it] 本帖最后由 baby69yy2000 于 2008-5-26 11:48 编辑 [/it]]

2008-05-26 11:44
快速回复:为什么一加这段就undefined呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029271 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved