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. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  2. C++ 的template

    vector的标准模板是:template<template<typename X, class allocator<X> > class T>而普通模板则是tem ...

  3. 结构体的malloc与数组空间

    结构体的malloc 如果结构体中有指针,对结构体的malloc 和其指针成员变量的malloc是没有关系的 结构体malloc的是存储自己地址的 忘记了面试常考试的sizeof的几个主要点 ==== ...

  4. MySQL忘记密码后重置密码(Mac )

    安装好MySQL以后,系统给了个默认的的密码,然后说如果忘记了默认的密码......我复制了默认密码就走过了这一步,这一步就是我漫长旅程的开始.他给的密码太复杂了,当然我得换一个,而且我还要假装我不记 ...

  5. [转载]mysql慢日志文件分析处理

    原文地址:mysql慢日志文件分析处理作者:maxyicha mysql有一个功能就是可以log下来运行的比较慢的sql语句,默认是没有这个log的,为了开启这个功能,要修改my.cnf或者在mysq ...

  6. 在.NET连接MySQL以及封装好的MySQLHelper.cs

    1.首先上MySQL网站下驱动:http://www.mysql.com/products/connector/ 2.安装下载的安装包 3.我们在Visual Studio里创建一个Web Appli ...

  7. ASP.net中导出Excel的简单方法介绍

    下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...

  8. JavaScript的常见事件和Ajax小结

    一.常见事件类型 1.鼠标事件 事件名称 说明 onclick 鼠标单击时触发 ondbclick 鼠标双击时触发 onmousedown 鼠标左键按下时触发 onmouseup 鼠标释放时触发 on ...

  9. GridView分页排序

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewPage.asp ...

  10. 数据可视化(一)-Matplotlib简易入门

    本节的内容来源:https://www.dataquest.io/mission/10/plotting-basics 本节的数据来源:https://archive.ics.uci.edu/ml/d ...