Javascript 需要减少双队列代码

Javascript 需要减少双队列代码,javascript,Javascript,有没有什么方法可以减少这段代码来做同样的事情,但减少100个字符 这是一个简单的双边队列,包含pushHead、pophhead、pushTail、popTail,以及访问长度和isEmpty的方法 var makeDeque = function() { var a= []; this.length= a.length=0; this.pushHead=function(v) { a.unshift(v); } this.pop

有没有什么方法可以减少这段代码来做同样的事情,但减少100个字符

这是一个简单的双边队列,包含pushHead、pophhead、pushTail、popTail,以及访问长度和isEmpty的方法

var makeDeque = function()
{
    var a= [];
    this.length= a.length=0;

    this.pushHead=function(v)
    {
        a.unshift(v);
    }
    this.popHead=function()
    {
        return a.shift();
    }

    this.pushTail=function(v)
    {
        a.push(v);
    }

    this.popTail=function()
    {
    return a.pop();
    }

    this.isEmpty=function()
    {
        return a.length===0;
    }

    return this;
};

谢谢

您可以摆脱手动数组处理。我想你不能得到比这更短的代码(你可以缩短变量名,但代码可能至少需要这么长)

这样,您还可以获得默认
数组的所有功能
Deque
在本例中,实际上是
Array
类的一个子类。

请查看
function Deque() {}
Deque.prototype = new Array();
var prot = Deque.prototype;
prot.pushHead = Deque.prototype.unshift;
prot.popHead = Deque.prototype.shift;
prot.pushTail = Deque.prototype.push
prot.popTail = Deque.prototype.pop
prot.isEmpty = function() {return this.length == 0}