I have a case where I want to do this:
var els = {
div: $('div'),
p: els.div.find('p'),
span: els.p.find('span')
};
But this happens:
console.log(els.div); // Works
console.log(els.p); // undefined
So I’m currently doing this:
var els = (function(){
var div = $('div'),
p = div.find('p'),
span = p.find('span');
return {
div: div,
p: p,
span: span
}
}());
console.log(els.p); // Works now
Is there any way I can make it DRYer? Seems like a bunch of code to just be able to do that.
You could build it step by step:
Or like this:
if it seems more consistent to you.