• Tested code in Emulator

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

    Tested code in Google Chrome

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

    Workaround in Espruino

    1. a = [0,1,2,3,4]
    2. //=[ 0, 1, 2, 3, 4 ]
    3. a = a.slice(0,3)
    4. //=[ 0, 1, 2 ]
    5. a.length
    6. //=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
    • $ Donate
About

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

Posted by Avatar for MaBe @MaBe

Actions