New array (length) gives wrong size

Update: This bug affects v28 and has been fixed for v29.


Having such a function:

function arrMany(len) {
    var a = new Array(len);
}

If a function is called quickly, the length of the created array sometimes has the length of the previous function call.

Function extension to:

function arrMany(len, show) {
    var a = new Array(len);
    if (show) {
        someElementTextA.value = a.length;
        someElementTextB.value = len;
    }
}

And a call, for example:

function tick() {
    arrMany(2);
    arrMany(4);
    arrMany(6);
    arrMany(10, true);
}

setInerval(tick, 1000 / 200);

after a while it someElementTextAshows the value 6, not 10.

When this happens, and one cancels fps to, for example, 1, it spills out the wrong length for a long time.


Behavior usually appears after about 20 seconds on my system.

Tested: Firefox Linux i686; sv: 28.0

Notes:

  • No action in the browser console or web console.
  • - , .
  • - , .
  • pt. 3. .

?


1:

Firefox, Chrome Opera. Firefox - , . .

: Window XP, Firefox v31. @jdigital - .


2:

show .

function tick() {
    arrMany(2, false);
    arrMany(4, false);
    arrMany(6, false);
    arrMany(10, true);
}

. , show .


, :

if (arr.length !== len) { arr = new Array(len); }

, . , , .

if (arr.length !== len) { arr = new Array(len); retry = 1; }

, .


( ); -. , . : retry 0.

for (retry = 0; arr.length !== len && retry < 10; ++retry) {
    arr = new Array(len);
}

.

  • , , .

, , - , . , , , .


3:

, " ", , .

, , , , - , , . , .

, , , , .

; , , ...;)

+4
3

:

function arrMany(w, show) {
    (function () {
        var a = new Array(w);
        if (show) {
            arw1.value = a.length;
            arw2.value = w;
        }
    })();
}

: JavaScript Strange Threaded Nature

:

(function() { new Object(); })();

+2

Firefox. Chrome ..
:

function arrMany(w, show) {
    var a = new Array(w);
    if (show) {
        if( a.length != w ) {
            console.log(a);
        }
        arw1.value = a.length;
        arw2.value = w;
    }
}

, , console.log , Firefox Web Console , 5 .

+1

If I understand correctly, you need a workaround for this error, even if it is an ugly hack, if it is reliable. If so, consider this approach, which explicitly initializes the array:

function arrMany(w, show) {
    // should be:
    //      var a = new Array(w);
    // but there a bug in Firefox, see StackOverflow 22726716
    //
    var a = [];
    for( var i = 0; i < w; i++ ) a[i] = undefined;

    if (show) {
        arw1.value = a.length;
        arw2.value = w;
    }
}
+1
source

Source: https://habr.com/ru/post/1533994/


All Articles