Loading ...
Sorry, an error occurred while loading the content.

bug in event.js?

Expand Messages
  • Peter Michaux
    Hi, I ve been reading the event.js file and found something suspicious. In the attached _unload function there is the line if (listeners && listeners.length
    Message 1 of 4 , Aug 31, 2006
    • 0 Attachment
      Hi,

      I've been reading the event.js file and found something suspicious. In
      the attached _unload function there is the line

      if (listeners && listeners.length > 0) {

      but there isn't a local variable in the function called "listeners".
      Should it really be...

      if (this.listeners && this.listeners.length > 0) {

      This is supposed to be the workaround for IE memory leaks, correct?

      -Peter


      _unload: function(e, me) {
      for (var i=0,len=unloadListeners.length; i<len; ++i) {
      var l = unloadListeners[i];
      if (l) {
      var scope = (l[this.ADJ_SCOPE]) ? l[this.SCOPE]: window;
      l[this.FN].call(scope, this.getEvent(e),
      l[this.SCOPE] );
      }
      }

      if (listeners && listeners.length > 0) {
      var j = listeners.length;
      while (j) {
      var index = j-1;
      l = listeners[index];
      if (l) {
      this.removeListener(l[this.EL], l[this.TYPE],
      l[this.FN], index);
      }

      j = j - 1;
      }

      this.clearCache();
      }

      for (i=0,len=legacyEvents.length; i<len; ++i) {
      delete legacyEvents[i][0];
      delete legacyEvents[i];
      }

      },
    • Adam Moore
      ... No, this is a private variable declared near the top of the file, and used throughout the utility. -Adam
      Message 2 of 4 , Aug 31, 2006
      • 0 Attachment
        On Thu, Aug 31, 2006 at 03:52:48PM -0700, Peter Michaux wrote:
        > Hi,
        >
        > I've been reading the event.js file and found something suspicious. In
        > the attached _unload function there is the line
        >
        > if (listeners && listeners.length > 0) {
        >
        > but there isn't a local variable in the function called "listeners".
        > Should it really be...
        >
        > if (this.listeners && this.listeners.length > 0) {

        No, this is a private variable declared near the top of the file, and used
        throughout the utility.

        -Adam
      • Peter Michaux
        ... Hi Adam, Thanks. I can see what is going on now. It s a bit different than how I do things. Maybe you guys were playing a trick to get JSDoc to pick up the
        Message 3 of 4 , Aug 31, 2006
        • 0 Attachment
          On 8/31/06, Adam Moore <adamoore@...> wrote:
          >
          > On Thu, Aug 31, 2006 at 03:52:48PM -0700, Peter Michaux wrote:
          > > Hi,
          > >
          > > I've been reading the event.js file and found something suspicious. In
          > > the attached _unload function there is the line
          > >
          > > if (listeners && listeners.length > 0) {
          > >
          > > but there isn't a local variable in the function called "listeners".
          > > Should it really be...
          > >
          > > if (this.listeners && this.listeners.length > 0) {
          >
          >
          > No, this is a private variable declared near the top of the file, and used
          > throughout the utility.
          >

          Hi Adam,

          Thanks. I can see what is going on now. It's a bit different than how
          I do things. Maybe you guys were playing a trick to get JSDoc to pick
          up the documentation since it can't document objects? I was told by
          someone in the JSDoc camp that they are going to change things so the

          var foo = function(){}();

          trick doesn't have to be used. It may take a long time for them to get
          there however.

          Thanks again,
          Peter
        • isaac_schlueter
          ... Peter, I don t think it s a trick for JSDoc. It s a closure, and it s how you make private variables in Javascript.
          Message 4 of 4 , Aug 31, 2006
          • 0 Attachment
            --- In ydn-javascript@yahoogroups.com, "Peter Michaux"
            <petermichaux@...> wrote:
            > Maybe you guys were playing a trick to get JSDoc to pick
            > up the documentation since it can't document objects?

            Peter,

            I don't think it's a trick for JSDoc. It's a closure, and it's how
            you make private variables in Javascript.

            http://javascript.crockford.com/private.html

            //------------//
            (function(){
            var a='private';
            YAHOO.namespace('myObj').getA = function(){ return a; };
            })();
            alert(YAHOO.myObj.getA()); // "private"
            alert(a); // Exception: "a is not defined"
            a=2;
            alert(YAHOO.myObj.getA()); // "private" -- window.a !== private a
            //------------//

            Note that now objects outside the closure can't overwrite the value of
            a. There's no worry that my variable named "a" might overwrite the
            value of your script's variable named "a". Careful scoping is a best
            practice, especially when writing code for mass consuption.

            --
            Isaac Z. Schlueter
            Webdev, Yahoo! Games
          Your message has been successfully submitted and would be delivered to recipients shortly.