I have a Google Map, and I am adding event listeners to different things. For instance, I have a Point object, and for this object, I have been adding events as thus:
google.maps.event.addListener(this.marker, 'click', (function(point) {
return function(event) {
alert(point.index);
}})(this));
I have a lot of these events (one for 'click', 'rightclick', 'doubleclick', etc).
When I am adding the event, I create a closure specifically around only the current point. What I am tempted to do however is just:
var point = this;
google.maps.event.addListener(this.marker, 'click', function(event) {
alert(point.index);
});
I have been avoiding this though because of two reasons.
One is that I've seen people that know more about Javascript than I do using "individual" closures, so I figure they must have a good reason.
The second is because (and I don't know anything about how Javascript is interpreted) I wonder if creating a large closure captures all of the other variables I'm not going to use in my event function (e.g. 'var color'). Could this cause performance problems?
Thanks for the help!