In my javascript code, I have a self executing anonymous function which executes immediately. Inside that I have document.ready() which makes sure that the dom is ready before doing stuffs. Just wondering whether the document.ready in my code is redundant or not.
(function() {
"use strict";
var app = {
init: function () {
app.addLun('Hello');
$('#some_id').on('click', this.changeStuff);
},
changeStuff: function(e) {
e.preventDefault();
$('#some_id').text("Cool text");
},
addLun: function(a) {
console.log(a);
}
};
$(document).ready(function() {
app.init();
});
})();
initmethod accesses the DOM, so it needs it to be ready. Worst case scenario is that the DOM is already ready (somehow) and theapp.init();executes immediately. Nothing wrong with that. Especially since I would assume that because the method is calledinit, it would only be called once...so it's not like the$(document).ready(part would be executed many times$(document).ready(function(){...});multiple times (e.g. within a loop) will cause the inner function to be called multiple times.. I've seen it happen on SO several times, so I thought I'd just throw that out there lol.