Phil meinte:
Consider the following code:
var pageEl=document. getElementById('someElement');
var foo='This is the right value of foo to use';
pageEl.addEventListener('click',function(evtObj){s omeClick(evtObj,foo);});
foo='This is the WRONG value of foo to use';
How do I get someClick to always use "the right value of foo"?
pageEl.addEventListener('click',function(evtObj){s omeClick(evtObj,"This
is the right value of foo to use");});
Well, one could use a second variable, after all they are not *that*
expensive...
There's no "simple" solution for your "problem" (I frequently use
callbacks, but never had a comparable problem). When the callback
function gets invoked it uses the current value of foo - after all
that's the behaviour that - I suppose - all people (except you) want to
see. The normal situation looks more like this
var foo;
addListener(function() { callback(foo); });
to establish the closure and have a value assigned to foo later
somewhere else.
Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum