This article demonstrates building a command-line physics calculator using JavaScript's IIFE (Immediately Invoked Function Expression) module pattern. It breaks down the creation into three modular JavaScript files and a package.json
file for managing the project.
The core functionality is divided into three modules:
iifePhysicsCalculations.js
: This module, implemented as an IIFE, encapsulates the physics calculation functions. It defines constants like gravity and the speed of light, keeping them private within the module. The functions for velocity, acceleration, potential energy, momentum, energy, force, and kinetic energy are exposed as methods of the returned object. Getter methods for the gravity constant and speed of light are also included.<code class="language-javascript">//iifePhysicsCalculations.js export const physicsCalculations = ( function () { // Private constants const g = 9.80665; const c = 299792458; // Public methods const velocity = (distance, time) => distance / time; const acceleration = (speed, time) => speed / time; const potentialEnergy = (mass, height) => mass * g * height; const momentum = (mass, speed) => mass * speed; const energy = (mass) => mass * (c ** 2); const force = (mass, acc) => mass * acc; const kineticEnergy = (mass, speed) => 0.5 * mass * (speed ** 2); return { velocity, acceleration, potentialEnergy, momentum, energy, force, kineticEnergy, getSpeedOfLight: () => c, getGravityConstant: () => g }; } )();</code>
PhysicsEquations.js
: This module exports an array of objects, each representing a physics equation. Each object contains the equation's ID, calculation formula, input descriptions, and units. This cleanly separates the equation definitions from the calculation logic.<code class="language-javascript">//PhysicsEquations.js export const physicsEquations = [ // ... (Equation objects as in the original code) ];</code>
PhysicsCalculationsApp.js
: This is the main application file. It imports the other two modules, uses node:readline
for command-line interaction, and orchestrates the user interface and calculation process. It presents a menu of physics equations, prompts the user for input, performs the calculations using the functions from iifePhysicsCalculations.js
, and displays the results.<code class="language-javascript">//PhysicsCalculationsApp.js import { physicsCalculations } from "./iifePhysicsCalculations.js"; import { physicsEquations } from "./PhysicsEquations.js"; import readline from 'node:readline'; // ... (rest of the code as in the original)</code>
A package.json
file with "type": "module"
is necessary to enable ES modules support in Node.js. The application then uses a while
loop and promises to handle user input and output in a clean and efficient manner. Error handling (e.g., for non-numeric input) could be added for robustness.
The above is the detailed content of Building a Command Line Physics Calculations Application with an IIFE Module Pattern. For more information, please follow other related articles on the PHP Chinese website!