The Full Stack Developer by Chris Northwood

The Full Stack Developer by Chris Northwood

Author:Chris Northwood
Language: eng
Format: epub
ISBN: 9781484241523
Publisher: Apress


JavaScript Modules

For a long time, JavaScript didn’t employ the concept of modules—simply different files that all acted in the same way in the same environment. JavaScript doesn’t have a standard library like other languages—all functions are available at all times—and the DOM was a single global object called window that new APIs kept getting added to over browser releases. If you wanted to bring in other functions that weren’t built into the language, you would normally add another <script> tag to your HTML before your code was loaded, and then those libraries would at best add something else to the window object (like JQuery), and at worst leak a bunch of internal functions everywhere, and you would just have to hope they didn’t clash with anything else.

The very first solution to that was to take advantage of JavaScript’s scoping rules to only expose what you wanted to leak. One way to do this is a mechanism known as an immediately-invoked function expression, or IIFE. When something is defined inside of a function, it only remains visible inside of that function, so to avoid putting everything onto window, modules were instead wrapped in a function that then became immediately invoked:(function() {

function somethingPrivate() { ... }

function externallyUsableFunction() { ... }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.