473,396 Members | 1,894 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,396 software developers and data experts.

how to pass a parameter to some bound function?

Max
i have a event bind function like this(though it is not so robust):

bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};

some usage like this:

bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );

but how can I pass a parameter to the bound function "fn" ?
Jun 27 '08 #1
6 2054
On May 28, 10:57 am, Max <Maximus.Zh...@gmail.comwrote:
i have a event bind function like this(though it is not so robust):

bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);

};

some usage like this:

bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );

but how can I pass a parameter to the bound function "fn" ?
Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.

For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:

function (evt) { myhandler(evt, "bar"); }
Jun 27 '08 #2
Max
On 5ÔÂ29ÈÕ, ÉÏÎç8ʱ15·Ö, "david.karr" <davidmichaelk....@gmail.comwrote:
On May 28, 10:57 am, Max <Maximus.Zh...@gmail.comwrote:
i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};
some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?

Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.

For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:

function (evt) { myhandler(evt, "bar"); }
thanks for reply.

may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like

document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.
Jun 27 '08 #3
VK
On May 29, 4:56 pm, Max <Maximus.Zh...@gmail.comwrote:
On 5ÔÂ29ÈÕ, ÉÏÎç8ʱ15·Ö, "david.karr" <davidmichaelk...@gmail.comwrote:
On May 28, 10:57 am, Max <Maximus.Zh...@gmail.comwrote:
i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};
some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?
Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.
For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:
function (evt) { myhandler(evt, "bar"); }

thanks for reply.

may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like

document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.
On event listener call there are two "persistent" objects available:
1) the bound HTML element referred by "this"
2) the event handler referred by "arguments.callee"
The first one can be used to store element-specific extra info. There
is also Function constructor - this is what I am using most often -
and closures which is an awful way IMO to use for identical event
handlers.

-- Option 1 --

refHtmlElement.
addEventListener('click',
new Function(txt, functionBody),
false);

-- Option 2 --
refHtmlElement.
addEventListener('click',
myFunction,
false);
refHtmlElement.args = {
'txt' : txt
};

and then later

function myFunction(evt) {
var txt = this.args.txt;
// ...
}
Jun 27 '08 #4
Max
On 5ÔÂ29ÈÕ, ÏÂÎç9ʱ40·Ö, VK <schools_r...@yahoo.com>wrote:
On May 29, 4:56 pm, Max <Maximus.Zh...@gmail.comwrote:
On 5ÔÂ29ÈÕ, ÉÏÎç8ʱ15·Ö, "david.karr" <davidmichaelk...@gmail.comwrote:
On May 28, 10:57 am, Max <Maximus.Zh...@gmail.comwrote:
i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};
some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?
Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.
For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:
function (evt) { myhandler(evt, "bar"); }
thanks for reply.
may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like
document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.

On event listener call there are two "persistent" objects available:
1) the bound HTML element referred by "this"
2) the event handler referred by "arguments.callee"
The first one can be used to store element-specific extra info. There
is also Function constructor - this is what I am using most often -
and closures which is an awful way IMO to use for identical event
handlers.

-- Option 1 --

refHtmlElement.
addEventListener('click',
new Function(txt, functionBody),
false);

-- Option 2 --
refHtmlElement.
addEventListener('click',
myFunction,
false);
refHtmlElement.args = {
'txt' : txt

};

and then later

function myFunction(evt) {
var txt = this.args.txt;
// ...

}

thanks VK
I was testing.....
but if the addEventListener is used in some class and the
refHtmlElement should be document, then the way in option2 you
mentioned just not work.
class like:
idle=function(callback){
this.starttime=new Date();
this.bound=false;
this.fn=callback;
this.reset=function(){
this.starttime=new Date();
};
this.init=function(){
if(!this.bound){
document.
addEventListener('mousemove',
this.reset,
false);
this.bound=true;
}
var secs=((new Date())-this.starttime)/1000;
if( secs % 59 == 0 ) {
this.fn();
}
}
....
};
....
/*
myidle=new idle(some function);
setInterval('myidle.init();',1000);
*/
i want to indicate (by mousemove event) if the user
left the page for every 60 seconds, and
the function fn is tiggered.
and by reset the starttime to new Date() while the user is
still active on the page.(also by mousemove event)
but after event bound, in the this.reset function,
this is no more refer to the object i create (eg. myidle),
~~~~
but refer to document instead.
I should use document for the "refHtmlElement", do you
think not?

any solutions?
Jun 27 '08 #5
how about this?

var idle=function(callback){
var me=this;
* * * * me.starttime=new Date();
* * * * me.bound=false;
* * * * me.fn=callback;
* * * * me.reset=function(){
* * * * * * * * me.starttime=new Date();
* * * * * * * * };
* * * * me.init=function(){
* * * * * * * * if(!me.bound){
* * * * * * * * * * * * document.
* * * * * * * * * * * * addEventListener('mousemove',
* * * * * * * * * * * * * * * * me.reset,
* * * * * * * * * * * * * * * * false);
* * * * * * * * * * * * me.bound=true;
* * * * * * * * }
* * * * * * * * var secs=((new Date())-me.starttime)/1000;
* * * * * * * * if( secs % 59 == 0 ) {
* * * * * * * * * * * * me.fn();
* * * * * * * * }
* * * * }
* * * * ....};

....
/*
myidle=new idle(some function);
setInterval('myidle.init();',1000);
*/
Jun 27 '08 #6
Max
On 5ÔÂ30ÈÕ, ÉÏÎç12ʱ07·Ö, RoLo <roloswo...@gmail.comwrote:
how about this?

var idle=function(callback){
var me=this;
me.starttime=new Date();
me.bound=false;
me.fn=callback;
me.reset=function(){
me.starttime=new Date();
};
me.init=function(){
if(!me.bound){
document.
addEventListener('mousemove',
me.reset,
false);
me.bound=true;
}
var secs=((new Date())-me.starttime)/1000;
if( secs % 59 == 0 ) {
me.fn();
}
}
....};

....
/*
myidle=new idle(some function);
setInterval('myidle.init();',1000);
*/
Yeah, it works very nicely.

Thank you all very much!
Jun 27 '08 #7

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

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common...
7
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
2
by: Ranginald | last post by:
Hi, I have a repeater based on a stored procedure. It pulls the following: catDesc Image The user sees the category description (catDesc) , and then clicks on the image (Image) which is...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.