本文共 1099 字,大约阅读时间需要 3 分钟。
/** * [Queue] * @param {[Int]} size [队列大小] */function Queue(size) { var list = []; //向队列中添加数据 this.push = function(data) { if (data==null) { return false; } //如果传递了size参数就设置了队列的大小 if (size != null && !isNaN(size)) { if (list.length == size) { this.pop(); } } list.unshift(data); return true; } //从队列中取出数据 this.pop = function() { return list.pop(); } //返回队列的大小 this.size = function() { return list.length; } //返回队列的内容 this.quere = function() { return list; }}//初始化没有参数的队列var queue = new Queue();for (var i = 1; i <= 5; i++) { queue.push(i);}console.log(queue.quere());console.log(queue.pop()); //从队列中取出一个console.log(queue.quere());var queue = new Queue(3);for (var i = 1; i <= 5; i++) { queue.push(i);}console.log(queue.quere());console.log(queue.pop());console.log(queue.quere());
结果:
[ 5, 4, 3, 2, 1 ]
1 [ 5, 4, 3, 2 ][ 5, 4, 3 ] 3 [ 5, 4 ]
厉害了 我的JS
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6709993.html如需转载请自行联系原作者