js中没有list类,可以使用Array来实现list类

(function(win) {
var ArrayList = function() {
this.datas = [];
}; var proto = ArrayList.prototype; proto.size = function() {
return this.datas.length;
}; proto.isEmpty = function() {
return this.size() === 0;
}; proto.contains = function(value) {
//return this.datas.indexOf(value) !== -1;
}; proto.indexOf = function(value) {
for ( var index in this.datas) {
if (this.datas[index] === value) {
return index;
}
} return -1;
}; proto.lastIndexOf = function(value) {
for ( var index = this.size(); index >= 0; index--) {
if (this.datas[index] === value) {
return index;
}
}
}; proto.toArray = function() {
return this.datas;
}; proto.outOfBound = function(index) {
return index < 0 || index > (this.size() - 1);
}; proto.get = function(index) {
if (this.outOfBound(index)) {
return null;
} return this.datas[index];
}; proto.set = function(index, value) {
this.datas[index] = value;
}; proto.add = function(value) {
this.datas.push(value);
}; proto.insert = function(index, value) {
if (this.outOfBound(index)) {
return;
} this.datas.splice(index, 0, value);
}; proto.remove = function(index) {
if (this.outOfBound(index)) {
return false;
} this.datas.splice(index, 1);
return true;
}; proto.removeValue = function(value) {
if (this.contains(value)) {
this.remove(this.indexOf(value));
return true;
}
return false;
}; proto.clear = function() {
this.datas.splice(0, this.size());
}; proto.addAll = function(list) {
if (!list instanceof ArrayList) {
return false;
} for ( var index in list.datas) {
this.add(list.get(index));
} return true;
}; proto.insertAll = function(index, list) {
if (this.outOfBound(index)) {
return false;
} if (!list instanceof ArrayList) {
return false;
} var pos = index;
for(var index in list.datas)
{
this.insert(pos++, list.get(index));
}
return true;
}; function numberorder(a, b) {
return a - b;
} proto.sort = function(isNumber){
if(isNumber){
this.datas.sort(numberorder);
return;
} this.datas.sort();
}; proto.toString = function(){
return "[" + this.datas.join() + "]";
}; proto.valueOf = function(){
return this.toString();
}; win.ArrayList = ArrayList;
})(window);

ArrayList.js

下面是测试页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ArrayList Test</title>
<script type="text/javascript" src="ArrayList.js"></script>
<script type="text/javascript">
window.print = function(value) {
//alert("window.print");
document.write(value);
}; window.println = function(value) {
//alert("window.println");
print(value);
document.write("<br/>");
}; var list = new ArrayList();
list.add("jack");
list.add(43);
list.add(true);
println("----------------------");
println(list.get(0));
println(list.get(1));
println(list.get(2));
println(list.get(3));
println("----------------------"); println(list.size()); list.remove(2);
println(list);
println("----------------------"); println(list.isEmpty());
list.clear();
println(list.isEmpty());
println("----------------------"); list.add("jack");
list.add(43);
list.add(true); var list2 = new ArrayList();
list2.addAll(list);
println(list2);
println("----------------------"); list2.insert(1,"male");
println(list2);
println("----------------------"); list2.removeValue(true);
println(list2);
println("----------------------"); list2.insertAll(2,list);
println(list2);
println("----------------------"); println(list2.contains("jack"));
println("----------------------"); list2.clear();
list2.add(1111);
list2.add(222);
list2.add(33);
list2.add(4); list2.sort();//按字母顺序排
println(list2);
println("----------------------"); list2.sort(true);//按数字顺序排
println(list2);
println("----------------------"); </script>
</head>
<body> </body>
</html>

ListTest.html

测试结果:

----------------------
jack
43
true
null
----------------------
3
[jack,43]
----------------------
false
true
----------------------
[jack,43,true]
----------------------
[jack,male,43,true]
----------------------
[jack,male,43,true]
----------------------
[jack,male,jack,43,true,43,true]
----------------------
undefined
----------------------
[1111,222,33,4]
----------------------
[4,33,222,1111]
----------------------

js 实现list类的更多相关文章

  1. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  2. 通过寄生组合式继承创建js的异常类

    最近项目中在做js的统一的异常处理,需要自定义异常类.理想的设计方案为:自定义一个异常错误类BaseError,继承自Error,然后再自定义若干个系统异常,例如用户取消异常.表单异常.网络异常,这些 ...

  3. JS中定义类的方法

    JS中定义类的方式有很多种: 1.工厂方式    function Car(){     var ocar = new Object;     ocar.color = "blue" ...

  4. js模块,类,继承,命名空间,私有属性等相关概念梳理

    js确切的说是一种基于对象的语言,和纯面向对象的语言(比如as)稍微有点区别,js中没有类的概念.虽然有继承但是基于原型的继承.随着前段越来越受重视,jser们利用js的一些特性他们制造出了和纯面向对 ...

  5. JS中定义类的方法<转>

    转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象 ...

  6. js集合set类的实现

    js集合set类的实现 /*js集合set类的实现*/ function Set() { this.dataStore = []; this.add = add;//新增元素 this.remove ...

  7. 使用js栈stack类的实现

    使用js栈stack类的实现 /*使用栈stack类的实现*/ function stack() { this.dataStore = [];//保存栈内元素,初始化为一个空数组 this.top = ...

  8. js中的类和对象以及自定义对象

    js中的类 1.类的声明 function Person(name,age){ this.name=name; this.age=age; this.test=function(a){ alert(a ...

  9. js中尺寸类样式

    js中尺寸类样式 一:鼠标尺寸类样式 都要事件对象的配合 Tip:注意与浏览器及元素尺寸分开,鼠标类尺寸样式都是X,Y,浏览器及元素的各项尺寸时Height,Width 1:检测相对于浏览器的位置:e ...

  10. koa 基础(十七)原生 JS 中的类、静态方法、继承

    1.app.js /** * 原生 JS 中的类.静态方法.继承 * es5中的类和静态方法 */ function Person(name, age) { // 构造函数里面的方法和属性 this. ...

随机推荐

  1. OpenGL7-3快速绘制(索引方式)

    代码下载#include "CELLWinApp.hpp"#include <gl/GLU.h>#include <assert.h>#include &l ...

  2. Php的安装与配置

    PHP的安装 php不需要安装,只是一个软件包,在Apache启动的过程中加载即可 PHP的配置 php是一个软件包,只需要在apache启动过程中加载即可,Php对于apache是一个功能模块. 测 ...

  3. TCP/IP:链路层

    链路层主要目的: 1.        为IP模块发送和接收IP数据报. 2.        为ARP模块发送ARP请求和接收ARP应答. 3.        为RARP发送RARP请求和接收RARP应 ...

  4. stl::find,find_if,find_if_not

    //满足特定条件下的实现,回调函数template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, ...

  5. Android学习1

    Activity学习(1) 只有一个Activity 进行Toast通知 Toast是一种短小的提醒,显示一段时间就会消失,试验学习,可以通过一个Button来实现. Button reg=(Butt ...

  6. JavaScript 防止事件冒泡

    在我们书写一个弹窗的时候,我们往往需要点击弹窗的其他地方来隐藏弹窗. 通常我们会写成: $(document).bind('click',function(){ $('.pop-box').hide( ...

  7. linux关闭服务的方法

    本文介绍下,在linux下关闭服务的方法,主要学习chkconfig的用法,有需要的朋友参考下. 先来看一个在linux关闭服务的例子,例如,要关闭sendmail服务,则可以按如下操作. 例1, 复 ...

  8. ShowMask

    <html> <head> <script type="text/javascript"> function showMask(){ var a ...

  9. PyQt4学习笔记2:事件和信号

    事件是任何 GUI 程序中很重要的部分.所有 GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网络通信,窗口 ...

  10. gc overhead limit exceeded

    eclipse-- gc overhead limit exceeded 修改内存不足的方法如下: Eclipse报错:gc overhead limit exceeded eclipse 原因是Ec ...