This simple API is a self improvement of the example given by You Don't Know Js book about closure and modules. In the closure and module section, the author made an example of a simple public API for user creation. After reading that, I was inspired to add some more publicAPI functions for more general use cases. This is the original code from the book:
function User(){
var username, password;
function doLogin(user,pw) {
username = user;
password = pw;
// do the rest of the login work
}
var publicAPI = {
login: doLogin
};
return publicAPI;
}
// create a `User` module instance
var fred = User();
fred.login( "fred", "12Battery34!" );
For those who don't yet understood what closure is, basically it is a javascript thing which can be used for a function to remember what was given in the first variable scope. I don't really know how to describe it using words, but I understand what it means by looking at codes that implement it.