Set length property of JavaScript object
<p>Let's say I have a JavaScript object:</p>

<pre><code>function a(){
    var A = [];
    this.length = function(){
        return A.length;
    };
    this.add = function(x){
        A.push(x);
    };
    this.remove = function(){
        return A.pop();
    };
};
</code></pre>

<p>I can use it like so:</p>

<pre><code>var x = new a();
x.add(3);
x.add(4);
alert(x.length()); // 2
alert(x.remove()); // 4
alert(x.length()); // 1
</code></pre>

<p>I was trying to make <code>.length</code> not a function, so I could access it like this: <code>x.length</code>, but I've had no luck in getting this to work.</p>

<p>I tried this, but it outputs <code>0</code>, because that's the length of <code>A</code> at the time:</p>

<pre><code>function a(){
    var A = [];
    this.length = A.length;
    //rest of the function...
};
</code></pre>

<p>I also tried this, and it also outputs <code>0</code>:</p>

<pre><code>function a(){
    var A = [];
    this.length = function(){
        return A.length;
    }();
    //rest of the function...
};
</code></pre>

<p>How do I get <code>x.length</code> to output the correct length of the array inside in the object?</p>
