473,842 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='addEventLi stener';
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 2082
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='addEventLi stener';
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.comwr ote:
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='addEventLi stener';
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.addEve ntListener( 'click', function(txt){a lert(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.comwro te:
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='addEventLi stener';
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.addEve ntListener( 'click', function(txt){a lert(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.call ee"
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.
addEventListene r('click',
new Function(txt, functionBody),
false);

-- Option 2 --
refHtmlElement.
addEventListene r('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...@y ahoo.com>wrote:
On May 29, 4:56 pm, Max <Maximus.Zh...@ gmail.comwrote:
On 5月29日, 上午8时15分, "david.karr " <davidmichaelk. ..@gmail.comwro te:
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='addEventLi stener';
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.addEve ntListener( 'click', function(txt){a lert(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.call ee"
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.
addEventListene r('click',
new Function(txt, functionBody),
false);

-- Option 2 --
refHtmlElement.
addEventListene r('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 addEventListene r 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(c allback){
this.starttime= new Date();
this.bound=fals e;
this.fn=callbac k;
this.reset=func tion(){
this.starttime= new Date();
};
this.init=funct ion(){
if(!this.bound) {
document.
addEventListene r('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('my idle.init();',1 000);
*/
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(c allback){
var me=this;
* * * * me.starttime=ne w Date();
* * * * me.bound=false;
* * * * me.fn=callback;
* * * * me.reset=functi on(){
* * * * * * * * me.starttime=ne w Date();
* * * * * * * * };
* * * * me.init=functio n(){
* * * * * * * * if(!me.bound){
* * * * * * * * * * * * document.
* * * * * * * * * * * * addEventListene r('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('my idle.init();',1 000);
*/
Jun 27 '08 #6
Max
On 5月30日, 上午12时07分, RoLo <roloswo...@gma il.comwrote:
how about this?

var idle=function(c allback){
var me=this;
me.starttime=ne w Date();
me.bound=false;
me.fn=callback;
me.reset=functi on(){
me.starttime=ne w Date();
};
me.init=functio n(){
if(!me.bound){
document.
addEventListene r('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('my idle.init();',1 000);
*/
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
3546
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 by pointer in C++, is there such way in Python? Thansk in advance. J.R.
110
9999
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 must be an object instead of
9
2320
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 function that was included in all pages that took an SQL query and returned a disconnected recordset. This meant that data access could be achieved in a single line. I would like to do something similar in ASP.NET. I know I could just duplicate...
7
2967
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 is address variable, which holds the memory address of the object. Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer. Why needs pass by reference?
2
4602
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 bound to the catID. This catID (@catID --- an integer) is then to be passed to the next page, sculpture2.aspx for use in a datagrid with a stored procedure.
5
7831
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 a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address. Surely when the WebService method is called and run 'on the server', the reference type will be...
14
20419
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
11
3371
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
12
11122
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9872
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
10945
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10612
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10314
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
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梡lanning, coding, testing, and deployment梬ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5696
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.