SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable losing scope after page load...

    Hi all.

    My goal here is to create a photo gallery.
    I have done some functions to load various things and add events to the scroll buttons etc. but when the page loads, all the data I set up seems to be lost or is out of scope.

    I have created some functions to demonstrate.

    Code:
    var a_var = {
      inner_var: 1,
    
      set: function(num) {
        this.inner_var = num;
      } ,
    
      view: function() {
        alert('here - ' + this.inner_var);
      }
    };
    
    a_var.set(9);
    //a_var.view();
    
    add_event(window, 'load' , a_var.view);
    
    
    /**************/
    
    function add_event(obj , event_type , func) {
      if(obj.addEventListener) {
        obj.addEventListener(event_type , func , false);
        return true;
      } else if(obj.attachEvent) {
        var r = obj.attachEvent('on' + event_type , func);
        return r;
      } else {
        return false;
      }
    }
    I have commented out the line
    a_var.view();

    because for some reason, if I run that view function then, the function doesn't run when the page is loaded. That's the first and not so important problem.

    The major problem is, when you do comment out that line, the function runs once the page is loaded, but inner_var is undefined.
    Does anyone know why that's happening?
    Any help will be appreciated, as I am a little stumped at the moment...

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this within view refers to the window object. A way to get around this since the object is not a class, is to use

    Code javascript:
    alert('here - ' + a_var.inner_var);


  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is the use of 'this'. When you assign a_var.view as an event handler to the window object, 'this' then points to window.

    If a_var is a singleton try being more specific:
    Code:
    var a_var = {
      inner_var: 1,
    
      set: function(num) {
        a_var.inner_var = num;
      } ,
    
      view: function() {
        alert('here - ' + a_var.inner_var);
      }
    };

  4. #4
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey awesome, continue learning something new on sitepoint every day.

    Thanks for the help peeps


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •