0
var budgetController = (function() {
var x = 23;

var add = function(a) {
    return x + a;
}

return {
    publicTest: function(b) {
        return add(b);
    }
}
})();

var budgetController = (function() {
var x = 23;

var add = function(a) {
    return x + a;
}

return {
    publicTest(b) {
        return add(b);
    }
}
})();

I'm learning an online course about Module and Closure. I'm confused with these two style of function format. If I call budgetController.publicTest(5); Both seems work. Thank you so much!

Ying
  • 299
  • 1
  • 5
  • 20
  • Regarding closures, there is no difference at all – Bergi Oct 24 '18 at 21:47
  • The function inside `return` is called method which is publically available when `budgetController ` instance is created using the `new` keyword. Whereas, the function above the return is private to `budgetController` and can only be accessed inside this controller. This is just to bring some OO concept into JavaScript but now in ES5+ there are classes available instead going with your approach. – null Oct 24 '18 at 22:19

0 Answers0