Mark
Mark

Reputation: 245

Variable inside an object context

How do I make nav_items work in the following context?

params = {
    nav: $('.nav'),
    nav_items: params.nav.find('li')
}

ReferenceError: params is not defined (logically true)

Also tried this:

params = {
    nav: $('.nav'),
    nav_items: nav.find('li')
}

ReferenceError: nav is not defined (what is the right way?)

I know, it can be done using this code:

params = {
    nav: $('.nav'),
    nav_items: null
}
params.nav_items = nav.find('li');

But its interesting, can it be done without an extra code?

Upvotes: 4

Views: 89

Answers (2)

closure
closure

Reputation: 7452

May be too late but could not refrain:

params = (function(a) { 
  return {nav: a, nav_items: a.find('li')};
})($('.nav'));

Upvotes: 2

Claudio Redi
Claudio Redi

Reputation: 68440

var $nav = $('.nav');
params = {
    nav: $nav,
    nav_items: $nav.find('li')
}

Upvotes: 4

Related Questions