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

YAHOO.util.Event.addListener breaking object properties

Expand Messages
  • visseraj
    If I use the YAHOO.util.Event.addListener to load an object instead of using the body onload method, the properties of my object are undefined. See the code
    Message 1 of 2 , Feb 1, 2007
    • 0 Attachment
      If I use the YAHOO.util.Event.addListener to load an object instead of using the body onload method, the properties of my object are undefined.  See the code below for a better explanation

      function DDPortal() {
                  this.columns = new Array();
                  this.columns.push(1,2,3);
                  this.widgets = new Array(new Array(),new Array(),new Array());
                  this.update = update;
              }
             
              function update() {
                  console.log('updating: ' + this.columns);
                  console.log('updating: ' + this.widgets);
              }
             
              av = new DDPortal;
              //YAHOO.util.Event.addListener(window, "load", av.update);
          </script>
         
      </head>
      <body onload="av.update();">


      The code as it stands will output:
      updating: 1,2,3
      updating: ,,

      but if I uncomment the addListener line and remove the body onload I get:
      updating: undefined
      updating: undefined


      does anyone have any ideas, b/c I would really like to use the addListener event and not the body onload event?

      cheers,
      Anton

    • tssha
      ... The scope of this in update() is window instead of av(the DDPortal object). Correcting the scope to av will output updating: 1,2,3 ... instead of
      Message 2 of 2 , Feb 1, 2007
      • 0 Attachment
        --- In ydn-javascript@yahoogroups.com, "visseraj" <visseraj@...> wrote:
        >
        > If I use the YAHOO.util.Event.addListener to load an object instead of
        > using the body onload method, the properties of my object are undefined.
        > See the code below for a better explanation
        >
        > function DDPortal() {
        > this.columns = new Array();
        > this.columns.push(1,2,3);
        > this.widgets = new Array(new Array(),new Array(),new
        > Array());
        > this.update = update;
        > }
        >
        > function update() {
        > console.log('updating: ' + this.columns);
        > console.log('updating: ' + this.widgets);
        > }
        >
        > av = new DDPortal;
        > //YAHOO.util.Event.addListener(window, "load", av.update);
        > </script>
        >
        > </head>
        > <body onload="av.update();">
        >
        >
        > The code as it stands will output:
        > updating: 1,2,3updating: ,,
        >
        > but if I uncomment the addListener line and remove the body onload I
        > get:
        > updating: undefinedupdating: undefined

        The scope of "this" in update() is window instead of av(the DDPortal
        object). Correcting the scope to av will output "updating: 1,2,3"...
        instead of undefined.

        Regards,
        Thomas
      Your message has been successfully submitted and would be delivered to recipients shortly.