javascript - What is the difference between a closure and a module? - Stack Overflow

I know that a module like this one:function User(){var username, password;function doLogin(user, pw){us

I know that a module like this one:

function User(){
    var username, password;

    function doLogin(user, pw){
        username = user;
        password = pw;
    };
    var publicAPI = {
        login: doLogin
    };
    return publicAPI;
}

has a closure inside of it: doLogin, and is remembering the values of the variables username and password that are inside User, what makes this a "closure". What I don't quite understand is if whenever we use a closure we are using the module pattern? or as soon as I save the function of the User in a variable like var User = function(){... is not a module... Please bear in mind I'm learning js.

I know that a module like this one:

function User(){
    var username, password;

    function doLogin(user, pw){
        username = user;
        password = pw;
    };
    var publicAPI = {
        login: doLogin
    };
    return publicAPI;
}

has a closure inside of it: doLogin, and is remembering the values of the variables username and password that are inside User, what makes this a "closure". What I don't quite understand is if whenever we use a closure we are using the module pattern? or as soon as I save the function of the User in a variable like var User = function(){... is not a module... Please bear in mind I'm learning js.

Share Improve this question asked Nov 20, 2015 at 11:39 Camilo SanchezCamilo Sanchez 1,2822 gold badges13 silver badges17 bronze badges 1
  • 2 The module pattern is using closures, not the other way round. Closures are an integral part of the language, modules are just a pattern that emerged from them (and there's a lot of other things you can do with closures). – Bergi Commented Nov 20, 2015 at 12:31
Add a ment  | 

2 Answers 2

Reset to default 6

You have implemented a factory function for Users.

You are "almost" using the revealing module pattern. Most people would say the revealing module pattern would need to be invoked immediately. Module patterns are usually used for code organisation. What you have here instead is an object factory.

Closures are used by the (revealing) module pattern (and others) to achieve the encapsulation of private state.

A closure is created whenever a function is defined. They are a language feature of JavaScript to make working with functions easier.

Closures are a way to close over(or capture) information or state from the enclosing scope. Basically it is a means of extending the lifetime of a variable.

Modules are a way to hide the implementation and state from outside world.

So, doLogin() is closing over both username and password and thus increasing their lifetime. And object refereed to by publicAPI is hiding the implementation of doLogin() from the user

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744585512a4582286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信