Question

This is basically the structure of my code (based on this basically):

var s,
NewsWidget = {

  settings: {
    numArticles: 5
  },

  init: function() {
    // kick things off
    s = this.settings;
  }

};

So basically this is is the expected usage.

My problems is that when I'm declaring settings and want them to be in relationship to each other I can't:

settings: {
    numArticles: 5,
    scienceAricles: numArticles - 3
}

This obviously don't work either because the object is still not defined (This doesn't actually make sense, but just to exemplify what I mean):

scienceAricles: this.numArticles - 3
scienceAricles: s.numArticles - 3

So the only way I can think of right now is to do something like this:

var NewsWidget = {};
NewsWidget.s = {};

NewsWidget.s.numArticles = 5;
NewsWidget.s.scienceArticles = NewsWidget.s.numArticles -3;

Live demo.

But I don't like this approach so much and our codebase is already based on the previous mentioned and linked architecture, so it would be a pretty big re-structure.

Am I missing something? What is the best way of doing this without changing the whole approach?

Was it helpful?

Solution

var numArticles = 5,
   s,
   NewsWidget = {

     settings: {
       numArticles: numArticles,
       scienceAricles: numArticles - 3
     },

     init: function() {
       // kick things off
       s = this.settings;
     }    
  };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top