4

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?

2 Answers 2

4
var $nav = $('.nav');
params = {
    nav: $nav,
    nav_items: $nav.find('li')
}
Sign up to request clarification or add additional context in comments.

9 Comments

you could wrap values in functions
@Steve: I don't see that adding an extra line is something to be worried about. I will use this as it is :)
$nav becomes a global, I'm trying to make it quite local.
@Steve var makes it local.
I would like to use it for more than 1 element, more than 10 extra elements can make code a bit messy.
|
2

May be too late but could not refrain:

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

3 Comments

@Steve: Saw it after you accepted the answer. Could not avoid answering as your excepted answer does not meet your own required criteria.
Inlining the functions would be a bad call according to I would like to use it for more than 1 element, more than 10 extra elements can make code a bit messy.
@asawyer: Can't agree with you more! But Steve's requirement was such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.