Skip to main content
deleted 32 characters in body
Source Link
ROMANIA_engineer
  • 57k
  • 30
  • 211
  • 207

Other commenters have already covered the semantic difference of the two variants above. I wanted to note a stylistic difference: Only the "assignment" variation can set a property of another object.

I often build javascriptJavaScript modules with a pattern like this:

(function(){
        var exports = {};

        function privateUtil() {
                ...
        }

        exports.publicUtil = function() {
                ...
        };

        return exports;
})();

With this pattern, your public functions will all use assignment, while your private functions use declaration.

(Note also that assignment should require a semicolon after the statement, while declaration prohibits it.)

Other commenters have already covered the semantic difference of the two variants above. I wanted to note a stylistic difference: Only the "assignment" variation can set a property of another object.

I often build javascript modules with a pattern like this:

(function(){
        var exports = {};

        function privateUtil() {
                ...
        }

        exports.publicUtil = function() {
                ...
        };

        return exports;
})();

With this pattern, your public functions will all use assignment, while your private functions use declaration.

(Note also that assignment should require a semicolon after the statement, while declaration prohibits it.)

Other commenters have already covered the semantic difference of the two variants above. I wanted to note a stylistic difference: Only the "assignment" variation can set a property of another object.

I often build JavaScript modules with a pattern like this:

(function(){
    var exports = {};

    function privateUtil() {
            ...
    }

    exports.publicUtil = function() {
            ...
    };

    return exports;
})();

With this pattern, your public functions will all use assignment, while your private functions use declaration.

(Note also that assignment should require a semicolon after the statement, while declaration prohibits it.)

Source Link
Sean McMillan
  • 10.1k
  • 6
  • 57
  • 65

Other commenters have already covered the semantic difference of the two variants above. I wanted to note a stylistic difference: Only the "assignment" variation can set a property of another object.

I often build javascript modules with a pattern like this:

(function(){
        var exports = {};

        function privateUtil() {
                ...
        }

        exports.publicUtil = function() {
                ...
        };

        return exports;
})();

With this pattern, your public functions will all use assignment, while your private functions use declaration.

(Note also that assignment should require a semicolon after the statement, while declaration prohibits it.)