Let's say I have a JavaScript object:
function a(){ var A = []; this.length = function(){ return A.length; }; this.add = function(x){ A.push(x); }; this.remove = function(){ return A.pop(); }; };
I can use it like this:
var x = new a(); x.add(3); x.add(4); alert(x.length());
I tried to make .length not a function, so I could access it like this: x.length , but I was unlucky that it worked.
I tried this, but it prints 0 , because the length of A at this time:
function a(){ var A = []; this.length = A.length;
I also tried this and it also prints 0 :
function a(){ var A = []; this.length = function(){ return A.length; }();
How do I get x.length to output the correct length of an array inside an object?
Rocket hazmat
source share