JavaScript Self executing Anonymous functions in javascript self executing anonymous function,self executing anonymous async function,self

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();
});
})();
Answer:1

Self-executing anonymous functions and jQuery's ready event handler have nothing to do with one another so no, it's not redundant.

Answer:2

In general, no. The immediately-invoked function expression will be invoked immediately, whereas $(document).ready can delay execution. If you know that that whole block of code will be executed after the DOM is ready, then sure, it's redundant, but that's probably not the case.

You can, however, replace your immediately-invoked function expression with passing the whole block to $(document).ready, e.g.:

$(document).ready(function() {
    "use strict";
    var app = {
        // ...
    };
    app.init();
});
Answer:3

If you don't write the $(document).ready part then you will be accessing $('#some_id') inside the init function without waiting for the $(document).ready event, which will not work naturally. With document.ready, it will work though and have the same results as the following code:

$(document).ready(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);
        }
    };

    app.init();
});
Answer:4

Consider the two sections of Javascript code, which accomplish the exact same thing: Version 1: document.getElementById("test").style.backgroundColor = "green"; document.getElementById("test").style....

Consider the two sections of Javascript code, which accomplish the exact same thing: Version 1: document.getElementById("test").style.backgroundColor = "green"; document.getElementById("test").style....

jquery click not working. Although, this issue has been discussed a lot, I can not find an answer that solves the problem for me. I have enclosed jQuery code in $(document).ready but vainly. here is ...

jquery click not working. Although, this issue has been discussed a lot, I can not find an answer that solves the problem for me. I have enclosed jQuery code in $(document).ready but vainly. here is ...

  1. jquery click does not work
  2. jquery click does nothing
  3. jquery click does not fire
  4. jquery trigger click does not work
  5. jquery click event does not work
  6. jquery button click does not work
  7. jquery click event does not bind
  8. jquery click doesn't work on mobile
  9. jquery checkbox click does not work

I need to use Firefox web console a lot and I want every time I open Firefox, the Web Console panel will display automatically. Currently I have to press Ctrl + Shift + K to open it. Is there any ...

I need to use Firefox web console a lot and I want every time I open Firefox, the Web Console panel will display automatically. Currently I have to press Ctrl + Shift + K to open it. Is there any ...

  1. open firefox console
  2. open firefox console linux
  3. firefox open console shortcut
  4. open console firefox android
  5. open console firefox mobile
  6. open console firefox mac
  7. firefox open console in new window
  8. firefox open console hotkey
  9. firefox open console log
  10. firefox open console tab
  11. firefox open console error
  12. open firefox developer console
  13. open firefox web console
  14. open javascript console firefox
  15. open browser console firefox
  16. open dev console firefox
  17. firefox open debug console
  18. firefox open java console
  19. selenium firefox open console
  20. open js console firefox

I have two lists: the first one is displayed on the left and the second on the right. I want that if I click on an element in the left, he is removed and he is prepended to the right list (and the ...

I have two lists: the first one is displayed on the left and the second on the right. I want that if I click on an element in the left, he is removed and he is prepended to the right list (and the ...

  1. jquery move element to another div
  2. jquery move element up
  3. jquery move element to new parent
  4. jquery move element after another
  5. jquery move element before another
  6. jquery move element position
  7. jquery move element out of parent
  8. jquery move element to top
  9. jquery move element on scroll
  10. jquery move element left
  11. jquery move element to body
  12. jquery move element up one level
  13. jquery move element before sibling
  14. jquery move element to end of container
  15. jquery move element to top of list
  16. jquery move element to end of parent
  17. jquery move element above sibling
  18. jquery move element to top of parent
  19. jquery move element on page
  20. jquery move element above another