Overload event handler in javascript -


i've got event handler in javascript i'd overload :

function overloadedhandler(event, myobject){     if(event){         var elem = event.currenttarget;     } else {         var elem = myobject.x;     }     //function logic }  $('.mydiv').on('click', overloadedhandler); 

if want call overloadedhandler function (not event handler), what's right way ?

1. overloadedhandler(null, obj); 2. overloadedhandler(undefined, obj); 3. overloadedhandler(false, obj); 

p.s. understand put //function logic part in function & call when needed outside of event handler, know if there's wrong above approach?

well indeed seems bit odd @ first sight, there nothing wrong. equivalent having argument optional, except event handling mechanism forces define event first argument cannot make optional.

to make close possible optional argument, set event undefined when calling not event handler, since value if optional argument. null ok guess, think false semantically wrong: want there no event, not event false.

one way make maybe cleaner call event argument made purpose. in example, be:

overloadedhandler({currenttarget: obj.x}, obj); 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -