As well as the closures, you can use function.bind
:
google.maps.event.addListener(marker, 'click', change_selection.bind(null, i));
passes the value of i
in as an argument to the function when called. (null
is for binding this
, which you don't need in this case.)
function.bind
was introduced by the Prototype framework and has been standardised in ECMAScript Fifth Edition. Until browsers all support it natively, you can add your own function.bind
support using closures:
if (!('bind' in Function.prototype)) {
Function.prototype.bind= function(owner) {
var that= this;
var args= Array.prototype.slice.call(arguments, 1);
return function() {
return that.apply(owner,
args.length===0? arguments : arguments.length===0? args :
args.concat(Array.prototype.slice.call(arguments, 0))
);
};
};
}