4

我如何nav_items在以下情况下工作?

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

ReferenceError: params is not defined (logically true)

也试过这个:

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

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

我知道,可以使用以下代码完成:

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

但它很有趣,可以在没有额外代码的情况下完成吗?

4

2 回答 2

4
var $nav = $('.nav');
params = {
    nav: $nav,
    nav_items: $nav.find('li')
}
于 2012-12-14T12:54:09.590 回答
2

可能为时已晚,但无法克制:

params = (function(a) { 
  return {nav: a, nav_items: a.find('li')};
})($('.nav'));
于 2012-12-14T13:46:33.340 回答