• Tested code in Emulator

    a = [0,1,2,3,4]
    //=[ 0, 1, 2, 3, 4 ]
    a.length
    //=5
    a.length = 3
    //=3
    a
    =[ 0, 1, 2, 3, 4, length: 3 ]
    // should return >a = [0,1,2]
    

    Tested code in Google Chrome

    a = [0,1,2,3,4]
    //(5) [0, 1, 2, 3, 4]
    a.length
    //5
    a.length = 3
    //3
    a
    //(3) [0, 1, 2]
    

    Workaround in Espruino

    a = [0,1,2,3,4]
    //=[ 0, 1, 2, 3, 4 ]
    a = a.slice(0,3)
    //=[ 0, 1, 2 ]
    a.length
    //=3
    
  • Thanks - yes, that's something I've known about for a while. I was sure there was an issue on GitHub for it but I can't see it now!

    Did you hit this when trying to run some existing code on Espruino?

    I've just tweaked Espruino to ensure that length is at least returned as a constant, so while it's not compliant, at least code that uses it will now error.

  • Did you hit this when trying to run some existing code on Espruino?

    No, just identified this in my personal code.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Shorten array by changing the length does not shorten the array in Espruino

Posted by Avatar for MaBe @MaBe

Actions