js 实现list类
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类的更多相关文章
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- 通过寄生组合式继承创建js的异常类
最近项目中在做js的统一的异常处理,需要自定义异常类.理想的设计方案为:自定义一个异常错误类BaseError,继承自Error,然后再自定义若干个系统异常,例如用户取消异常.表单异常.网络异常,这些 ...
- JS中定义类的方法
JS中定义类的方式有很多种: 1.工厂方式 function Car(){ var ocar = new Object; ocar.color = "blue" ...
- js模块,类,继承,命名空间,私有属性等相关概念梳理
js确切的说是一种基于对象的语言,和纯面向对象的语言(比如as)稍微有点区别,js中没有类的概念.虽然有继承但是基于原型的继承.随着前段越来越受重视,jser们利用js的一些特性他们制造出了和纯面向对 ...
- JS中定义类的方法<转>
转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象 ...
- js集合set类的实现
js集合set类的实现 /*js集合set类的实现*/ function Set() { this.dataStore = []; this.add = add;//新增元素 this.remove ...
- 使用js栈stack类的实现
使用js栈stack类的实现 /*使用栈stack类的实现*/ function stack() { this.dataStore = [];//保存栈内元素,初始化为一个空数组 this.top = ...
- js中的类和对象以及自定义对象
js中的类 1.类的声明 function Person(name,age){ this.name=name; this.age=age; this.test=function(a){ alert(a ...
- js中尺寸类样式
js中尺寸类样式 一:鼠标尺寸类样式 都要事件对象的配合 Tip:注意与浏览器及元素尺寸分开,鼠标类尺寸样式都是X,Y,浏览器及元素的各项尺寸时Height,Width 1:检测相对于浏览器的位置:e ...
- koa 基础(十七)原生 JS 中的类、静态方法、继承
1.app.js /** * 原生 JS 中的类.静态方法.继承 * es5中的类和静态方法 */ function Person(name, age) { // 构造函数里面的方法和属性 this. ...
随机推荐
- 验证hashmap非线程安全
http://www.blogjava.net/lukangping/articles/331089.html final HashMap<String, String> firstHas ...
- XPath 初步讲解
2016-05-05 XPath是JavaScript 中节点查找手段,ie9以后的版本才支持w3c标准,其他浏览器基本支持.在e8之前的浏览器,通过基于 activeX的xml dom对象实现. 为 ...
- Script 代码段
script代码段 1.script代码段的执行 在Javascript代码中,可以使用script作为基本标识,script代码段在运行过程中是分段解析与执行的. 2.script代码段执行流程 在 ...
- 2013山东省“浪潮杯”省赛 A.Rescue The Princess
A.Rescue The PrincessDescription Several days ago, a beast caught a beautiful princess and the princ ...
- C++编译期判断是否能够转型
#include <iostream> #include <vector> using namespace std; template<class T,class U&g ...
- IAR ERROR --- [Li006]
今天移植代码时遇到一个比较奇葩的问题,记录如下: Error[Li006]: duplicate definitions for "Uart3"; in "E:\IAR_ ...
- Linux定时任务crontab每三秒执行一次shell
第一种方法:当然首先想到的是写一个触发的脚本,在触发脚本中使用死循环来解决此问题,如下: cat kick.sh #!/bin/bash while : ;do /home/somedir/scrip ...
- Spark Streaming揭秘 Day17 资源动态分配
Spark Streaming揭秘 Day17 资源动态分配 今天,让我们研究一下一个在Spark中非常重要的特性:资源动态分配. 为什么要动态分配?于Spark不断运行,对资源也有不小的消耗,在默认 ...
- 爬虫学习之基于Scrapy的网络爬虫
###概述 在上一篇文章<爬虫学习之一个简单的网络爬虫>中我们对爬虫的概念有了一个初步的认识,并且通过Python的一些第三方库很方便的提取了我们想要的内容,但是通常面对工作当作复杂的需求 ...
- 【C++】GacLib——ListView.ViewSwitching
http://www.gaclib.net/Demos/Controls.ListView.ViewSwitching/Demo.html#FILESYSTEMINFORMATION_H