Why can't add btn.onRelease in a loop?

Hello, experts

I’m tring to add a onRelease event into every button I have, code like

for(var i = 1; i <= totalPage; i++){
    _root['button'+i].onRelease = function(){gotoPage(i-1)};
}    

why its doesnt work.
I have to do like following

    button1.onRelease = function(){gotoPage(0)};
    button2.onRelease = function(){gotoPage(1)};
    button3.onRelease = function(){gotoPage(2)};
    button4.onRelease = function(){gotoPage(3)};
    button5.onRelease = function(){gotoPage(4)};

any help would be appriciated!