By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

how to pass a parameter to some bound function?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.