Many libraries I've seen use a similar style to define their structure. Upon further inspection, I've noticed that the first self-invoking function is often related to Require.js or AMD systems, with 'factory' as an argument. My curiosity has been piqued by Require.js, although I've always leaned towards Browserify.
I find myself pondering why the main code is enclosed in parentheses at the end of the initial self-invoking function. Is this a closure or simply an anonymous function? I am intrigued to delve deeper into both concepts and explore the advantages they offer. From what I can gather inside the closure, the author passes a string
, this
, and a callback
.
Could adopting this approach provide my own library with a secure method of globalizing the main object, as shown in the example below using Please
?
(function( globalName, root, factory ) {
if ( typeof define === 'function' && define.amd ) {
define( [], factory );
}
else if ( typeof exports === 'object' ) {
module.exports = factory();
}
else{
root[globalName] = factory();
}
}('Please', this, function(){
I have set out on a quest to deeply understand JavaScript and craft my own miniature MVC architecture. Despite any naysayers claiming it's already been done before, I embrace the challenge as an opportunity for growth and learning.
If you happen to know of any valuable resources for building a JavaScript library, or better yet, an MVC library, I would greatly appreciate your insight.