i've been learning Javascript like for 8 months right now, 5 months ago i found job as a Front End developer and i've been getting envolved more deeply in javascript, that's a thing I love. Recently I noticed that my code looks so ugly because were a bunch of functions and global variables, so I started reading a little bit about design patterns. Now i come with something is working for me but I am not sure if is a good practice, anyway i would like you guys to take a look on the code and tell me what can I improve and what is the better way to star using modular pattern in javascript, also if you can suggest me some material to learn about modular pattern and Javascript.
JAVASCRIPT CODE:
var responsiveModule = {
settings: {
device: false,
button: $(".responsive-btn"),
target: $("nav ul"),
mobileClass: "toggle-menu",
bgImage: '<img class="background-image" src="img/background.jpg" alt="">',
bgImageSelector: $(".background-image"),
windowWidth: $(window).width(),
},
init: function(){
responsiveModule.checkDevice();
responsiveModule.replaceImages();
responsiveModule.bindFunctions();
responsiveModule.listenResize();
},
checkDevice: function(){
if(this.settings.windowWidth > 992){
this.settings.device = "desktop";
} else {
this.settings.device = "mobile";
}
},
bindFunctions: function(){
var buton = this.settings.button,
muelleBtn = this.settings.muelleBtn;
buton.on("click touchstart", function(e){
e.preventDefault();
responsiveModule.animateMenu(responsiveModule.settings);
});
},
animateMenu: function(settings){
var device = settings.device,
target = settings.target,
mobileAnimation = settings. mobileClass;
if(device == "mobile"){
target.toggleClass(mobileAnimation);
}
},
replaceImages: function(){
var bgContainer = $("#main-content"),
bgImage = responsiveModule.settings.bgImage,
device = responsiveModule.settings.device,
backgroundSelector = $(".background-image");
if(device == "desktop" && backgroundSelector.length == 0){
bgContainer.append(bgImage);
}else if(device == "mobile" && backgroundSelector.length == 1){
backgroundSelector.remove();
}
},
listenResize: function(){
$(window).on("resize", function(){
responsiveModule.checkDevice();
responsiveModule.replaceImages();
responsiveModule.settings.windowWidth = $(window).width();
});
}
}
var tooltipModule = {
settings: {
tooltipState: false
},
init: function(){
tooltipModule.bindUIfunctions();
},
bindUIfunctions: function(){
var device = responsiveModule.settings.device;
if(device == "mobile"){
$(".ship").on("click", function(e){
e.preventDefault();
tooltipModule.manageTooltip(e);
});
}else{
$(".muelle-item").addClass("desktop");
}
},
manageTooltip: function(e){
var tooltip = $(e.currentTarget).next(),
tooltips = $(".tooltip");
tooltips.removeClass("display");
tooltip.addClass("display");
}
}
$(document).on("ready", function(){
responsiveModule.init();
tooltipModule.init();
});