I'm trying to make sense of this code from another developer and my JavaScript knowledge is lacking. This function is supposed to take the header menu of a site and convert it into a mobile style menu.
I understand why jQuery is being passed in as $
. I don't understand how the variable CPCU
is being passed back into itself or why it's being passed back as CPCU || {}
. Can someone help me understand how the CPCU
variable is working in this situation?
var CPCU = (function (_cpcu, $) {
'use strict';
/**
* Mobile Menu
*/
var mmenu = _cpcu.Menu.mobile = _cpcu.Menu.mobile || {};
// Properties.
mmenu.id = '#mobile-menu';
mmenu.el = $('');
mmenu.api = {};
mmenu.button = '#header-content .menu.button';
mmenu.aniClass = 'animate';
mmenu.opts = {
slidingSubmenus: false
};
mmenu.config = {
classNames: {
selected: 'active'
}
};
// Methods.
mmenu.init = function () {
mmenu.el = $(mmenu.id);
// Move the active class to from the A to the LI, must happen before mmenu init.
$('#mobile-menu').find('a.active').parent('li').addClass('active');
// Now we can init the menu. otherwise it doesn't pick up the active LI.
mmenu.api = mmenu.el.mmenu(mmenu.opts, mmenu.config).data('mmenu');
mmenu.button = $(mmenu.button);
mmenu.button.data('lines', $('.line1,.line2,.line3'));
mmenu.button.click(function () {
mmenu.api.open();
});
mmenu.api.bind('open', function () {
mmenu.button.data('lines').addClass(mmenu.aniClass);
});
mmenu.api.bind('close', function () {
mmenu.button.data('lines').removeClass(mmenu.aniClass);
});
};
// Set up doc ready.
$(document).ready(function () {
mmenu.init();
});
return _cpcu;
})(CPCU || {}, jQuery);