JavaScript iterator function