数据结构和算法之-列表

列表的实现

<br>(function(window) {<br> var win = window,<br> _restore = {

listSize: function() {

return this.restore.length;

},

length: function() {

console.log(this.restore);
return this.restore.length;

},

clear: function() {

delete this.restore;
this.restore = [];
this.listIndex = 0;
return this;

},

toSting: function() {

return this.restore;

},

getElement: function(ele) {

var index = this.find(ele);
if (index > -1) {
return this.restore[index];
}
return null;

},

insert: function(ele, pos) {

this.listIndex++;
this.restore.splice(pos, 0, ele);
return this;

},

append: function(ele) {

this.listIndex++;
this.restore.push(ele);
return this;

},

remove: function(ele) {

this.listIndex--;
var index = this.find(ele);
this.restore.splice(index, 1);
return this;

},

front: function(ele) {

var index = this.find(ele);
this.restore.splice(index, 1);
this.restore.splice(0, 0, ele);
return this;

},

end: function(ele) {

var index = this.find(ele);
this.restore.splice(index, 1);
this.restore.splice(this.restore.length, 0, ele);
return this;

},

prev: function(ele) {

var index = this.find(ele);
this.restore.splice(index, 1);
this.restore.splice(index-1, 0, ele);
return this;

},

next: function(ele) {

var index = this.find(ele);
console.log(index);
this.restore.splice(index,1);
this.restore.splice(index + 1, 0, ele);
return this;

},

currPos: function(ele) {

return this.find(ele);

},

moveTo: function(ele, pos) {

var index = this.find(ele);
this.restore.splice(index,1);
this.restore.splice(pos, 0, ele)
return this;

},

find: function(ele) {

for (var i = 0, len = this.restore.length; i < len; ++i) {
  if (ele === this.restore[i]) {
    return i;
  }
}
  return -1;
}

}

function list() {

return new list.prototype.init();

}

list.fn = list.prototype ={

init: function() {
this.pos = 0;
this.listIndex = 0;
this.restore = [];
this.listSize = _restore.listSize;
this.length = _restore.length;
this.clear = _restore.clear;
this.toString = _restore.toSting;
this.getElement = _restore.getElement;
this.insert = _restore.insert;
this.append = _restore.append;
this.remove = _restore.remove;
this.front = _restore.front;
this.end = _restore.end;
this.prev = _restore.prev;
this.next = _restore.next;
this.currPos = _restore.currPos;
this.moveTo = _restore.moveTo;
this.find = _restore.find;
this._sort = _restore.sort;
}

};

list.fn.init.prototype = list.fn;
win.$ = win.list = list;
win.$$ = list();

})(window)

list.fn和list.fn.init.prototype = list.fn,初始化时候直接调用对象方法。就像function Foo(){}然后你要使用他作为构造函数,制造一个对象var foo = new Foo;再然后调用初始化方法foo.init();这样就显得比较麻烦,jquery源码,就是采用这种方式实现的。

<br><!DOCTYPE html><br><html lang="en"><br><head><br> <meta charset="UTF-8"><br> <meta name="viewport" content="width=device-width, initial-scale=1.0"><br> <meta http-equiv="X-UA-Compatible" content="ie=edge"><br> <title>Document</title><br> <script src="./列表的实现.js" charset="utf-8"></script><br></head><br><body><br> <script type="text/javascript">

var mylist = new list();
  mylist.append(1);
  mylist.append(2);
  console.log(mylist.length());

  var mylist1 = new list();
  mylist1.append(1);
  mylist1.append(2);
  mylist1.append(3);
  console.log(mylist1.length());

  $$.append("a");
    console.log($$.length());

</script>
</body>
</html>

相关推荐