473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing values to callbacks

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"?
Jul 26 '08 #1
2 1185
Phil wrote:
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(s) {
return function(evtObj) {
someClick(evtObj, s);
};
})(foo),
false);

This has been asked here before.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jul 26 '08 #2
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
Jul 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a...
5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
2
by: tony lock | last post by:
I am trying to call an unmanaged dll from c#, the function is asyncronous and expects a callback function and a parameter which is labelled lpvoid but which expects the address of the calling object,...
8
by: promko | last post by:
Hi! I need to call the following unmanged method: HFCI __cdecl FCICreate( void FAR* pv) I have written the next managed declaration: public static extern IntPtr FCICreate( IntPtr pv );
2
by: Mary Pegg | last post by:
No point re-inventing the wheel, so thought I'd see if anybody's got some good example code kicking around. (Please excuse trivial syntactical errors, it's all coming off the top of my head). ...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
4
by: Nathan Sokalski | last post by:
I have 2 private variables in my class that I need to remember the values of between postbacks (or actually callbacks, since I am using AJAX). I want to avoid using session variables, if possible....
6
AMaxin
by: AMaxin | last post by:
So I have been learning about MultiThreading in VB.NET, and I did up some code based on an example I found HERE Everything seemed to work wonderfully, EXCEPT when I started to get into performing...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.