For a new project I need to write a JavaScript library and I'm not sure how to structure it. I read many articles and questions here on stackoverflow yesterday.
I like to support public and private functions and classes. So here is my result:
(function(window) {
var Library = Library || {};
// Library namespace
Library = (function() {
function privateFunc() {
return "I'm a private functon in Library.";
}
var privateClass = function(param) {
var _param = param;
}
return {
constructor: Library,
publicFunc: function() {
return "I'm a publicFunc functon in Library.";
}
};
})();
// String namespace
_namespace('String');
Library.String = (function() {
function privateFunc() {
return "I'm a private functon in Library.String.";
}
return {
constructor: Library.String,
publicFunc: function() {
return "I'm a publicFunc functon in Library.String.";
},
publicClass: function(param) {
var _param = param;
}
};
})();
// global function
function _namespace(name) {
...
}
// register libary
window.Library= window.$L = Library;
})(window);
Is this a good way to structure a library or are there better ways? And how do I implement private and public functions for my privateClass/publicClass?
Thank you