how to pass a parameter to some bound function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max

    how to pass a parameter to some bound function?

    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" ?
  • david.karr

    #2
    Re: how to pass a parameter to some bound function?

    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"); }

    Comment

    • Max

      #3
      Re: how to pass a parameter to some bound function?

      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.

      Comment

      • VK

        #4
        Re: how to pass a parameter to some bound function?

        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;
        // ...
        }

        Comment

        • Max

          #5
          Re: how to pass a parameter to some bound function?

          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?

          Comment

          • RoLo

            #6
            Re: how to pass a parameter to some bound function?

            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);
            */

            Comment

            • Max

              #7
              Re: how to pass a parameter to some bound function?

              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!

              Comment

              Working...