如何在以下背景下使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');
但有趣的是,可以在没有额外代码的情况下完成吗?
答案 0 :(得分:4)
var $nav = $('.nav');
params = {
nav: $nav,
nav_items: $nav.find('li')
}
答案 1 :(得分:2)
可能为时已晚,但无法克制:
params = (function(a) {
return {nav: a, nav_items: a.find('li')};
})($('.nav'));