richie
richie

Reputation: 2478

Easily access class variable in function in global scope

I have an object:

var long_object_name = {
  'init':function(){

    this.properties.the_screen_size[0] = window.innerWidth;
    this.properties.the_screen_size[1] = window.innerHeight;

    window.addEventListener('resize', function(){
      long_object_name.properties.the_screen_size[0] = window.innerWidth;
      long_object_name.properties.the_screen_size[1] = window.innerHeight;
    }, false);
  },
  'properties':{
    'the_screen_size':[]
  }
}

So I want to store the screen size in a variable that changes when the user changes the window size.

The problem is that in the re-size event function I've to type the "long_object_name" to “reach” the "properties" object. So my question is, is there any way to “reach” the "properties" object in another way, e.g. like this.properties but this doesn't work in this case, right?

Upvotes: 0

Views: 151

Answers (2)

user1106925
user1106925

Reputation:

var long_object_name = {
  'init':function(){

    var ss = this.properties.the_screen_size;

    ss[0] = window.innerWidth;
    ss[1] = window.innerHeight;

    window.addEventListener('resize', function(){
      ss[0] = window.innerWidth;
      ss[1] = window.innerHeight;
    }, false);
  },
  'properties':{
    'the_screen_size':[]
  }
}

Upvotes: 1

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

var long_object_name = {
  'init':function(){

    var screenSize = this.properties.the_screen_size;

    screenSize[0] = window.innerWidth;
    screenSize[1] = window.innerHeight;

    window.addEventListener('resize', function(){
      screenSize[0] = window.innerWidth;
      screenSize[1] = window.innerHeight;
    }, false);
  },
  'properties':{
    'the_screen_size':[]
  }
}

Upvotes: 1

Related Questions