When to Use Anonymous JavaScript Functions Instead of Traditional Named Functions

In the following code example, using traditional named functions and anonymous functions does the same thing.

I read var functionName = function () {} vs function functionName () {} and understood how the first named function was defined at runtime, while the second anonymous function was defined at the time of parsing for the script block.

My question is specifically what conditions will make one approach more suitable than another?

function get1() {
    return 'Hello';
};

function alert1(data) {
    alert(data);
};

var get2 = function() {
    return 'Goodby';
};

var alert2 = function(data) {
    alert(data);
};


alert1(get1());

alert2(get2());
+4
source share
2 answers

, function logfoo function logbar return, , , / return.

var myModule = (function() {
    return {
        foo:  function() {
            logfoo();
        },

        bar: function() {
            logbar();
        }
    }

    function logfoo() {
        console.log('foo');
    }

    function logbar() {
        console.log('bar');
    }
})();
0

, , doesnt . .

:

0

Source: https://habr.com/ru/post/1526378/


All Articles