- Module Pattern was used before ES6 Modules.
- Goal: Encapsulate functionality, suport private data, expose public API and all this is achieved by using IIFEs.
IIFE: Ex. (function(){})();
// IIFE Goal: Create a new scope, return the data just once. All of data inside IIFE will be private as it would inside the fn scope. // Using this way, we don't need to call it separately. And it ensures its called only once. // IIFE is created only once, goal is 'NOT TO REUSE' by executing it multiple times. // Result of running an IIFE is stored, or else it will disappear simply. const ShoppingCart2 = (function(){ const cart = []; const shippingCost = 10; const totalPrice = 237; const totalQuantity = 10; const addToCart = function(product, quantity){ cart.push({product, quantity}); console.log(`${quantity} ${product} added to cart. Shipping cost is ${shippingCost}`); }; const orderStock = function(product, quantity){ console.log(`${quantity} ${product} ordered from supplier`); }; // Need to return something, in order to return a public API. For that, an object is returned containing stuff which needs to be made public. return { addToCart, cart, totalPrice, totalQuantity }; })(); // Everything inside the above module is private to the module. // The above fn returns the object mentioned inside return statement and assign it to the ShoppingCart2 variable mentioned at the start of fn. This IIFE is returned then long ago. // All this is possible because of closures. Hence, addToCart can acccess the cart variable. ShoppingCart2.addToCart('apple', 4); ShoppingCart2.addToCart('pizza', 5); ShoppingCart2; ShoppingCart2.shippingCost; // inaccessible.
The Module pattern has been working even before ES6 modules.
Disadv:
- Now if we want one module per file, like we have with ES6 modules then multiple scripts need to be created and linked inside the HTML file.
- The order of the script loading will also matter.
- All those variables will be living in global scope.
Beside native ES6 modules & module pattern, JS also supported other module systems which were not native to JS. Ex. AMD, CommmonJS
Ex. CommonJS modules are used in Node.js for all of its existance. Recently ES6 modules have been implemented in Node.js
All modules on npm repository still use the commonJS module system as npm was originally intended for node. Only later, npm became the repository for the whole JS world. Hence, we are basically stuck with CommonJS. So, CommonJS still needs to be paid attention to as its impt in Node.js
Just like ES6 module, 1 file is 1 module in CommonJS.
The commonJS code won't work in browser, but it will work in node.js
ES Modules will replace all module system eventually but as of now we need to use commonjs also.
export keyword is an object, which is not defined in our code as well as in browser.
// EXPORT export.addToCart = function(product, quantity){ cart.push({product, quantity}); console.log(`${quantity} ${product} added to cart. Shipping cost is ${shippingCost}`); }; // IMPORT: is similar to ES Modules but would use a require fn. // require is not defined in browser env but its defined in node env as its a part of commonjs const addToCart = require('./shoppingCart.js')
The above is the detailed content of Modules. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
