OK, now that you've explained what you mean by lazy caching, you can do this:
var navbarModule = (function($) {
var self = {};
var $login;
function getLogin() {
if (!$login || $login.length == 0) {
$login = $(".navbar-form");
return $login;
}
}
self.hideLogin = function() {
getLogin().hide();
};
self.showLogin = function() {
getLogin().show();
};
return self;
}(jQuery));
Note:
If you call .showLogin()
or .hideLogin()
before there is a .navbar-form"
, then nothing will happen. It will create an empty jQuery object and the .hide()
and .show()
methods will simply not do anything. If you call it again later, it will see that the jQuery object is empty and look for ".navbar-form"
again.
It is unclear why you think caching the login object is even required here. Give it an id value and just find it upon demand with document.getElementById()
and it will be plenty fast.