Subj : Re: spidermonkey bug or feature? watch on array.length To : Jens Thiele From : Brendan Eich Date : Thu Aug 26 2004 03:28 pm Jens Thiele wrote: > a=[]; > a.watch("length",f); > a.push(10); > > will print: > this[p]:0 p:length o:0 n:1 > this[p]:1 p:length o:1 n:1 > > => my function is called twice > > bug or feature? > > the nice feature of the "second version" is you are called again after > the element is added which allows you to inspect the added element > At this point, I'd say it's a feature. Array.prototype.push sets each argument as an element starting at length, ending at length + argc - 1, and then (because it's generic and the object on which it is called may not be an Array), it sets length to length + argc. So you'll get argc + 1 calls to the length setter, for an Array object. If you use Array.prototype.push on a non-array, you'll get just the one length set at the end, after the argc set-element operations that (because the object is not an Array) do not magically set length to be one greater than the greatest index set so far. /be .