mardi 27 septembre 2016

What happens if I iterate over the return from a function call?

function buildArray():Array {
    return new Array([1, 2, 3, 4]);
}

for each (var element:Number in buildArray()) {
    // Do stuff
}

I've managed to find a blog post saying that in the above case, the result of buildArray() will be cached, so it won't be called multiple times. When I step through in the debugger, it only gets called the first time, but I don't know that that behavior isn't implementation specific. So specific question:

Can it be guaranteed that a function called in a loop like this will only be evaluated the first time? And is there some official reference confirming this?

Aucun commentaire:

Enregistrer un commentaire