Skip to content

Latest commit

 

History

History
147 lines (131 loc) · 3.15 KB

File metadata and controls

147 lines (131 loc) · 3.15 KB
Copyright (c) 2019-
Author: Chaitanya Tejaswi (github.com/CRTejaswi)    License: MIT

Modular Programming

  • Example: Export/Import in JS

The math.js module defines an object with 4 methods to evaluate square-root, square and cube of a variable, and the rms value of two variables. The index.js script imports this object into its scope.

// math.js

// IIFE
(function (window){
    const math = {
        sqrt(x){
            return Math.sqrt(x);
        },
        square(x) {
            return x ** 2;
        },
        cube(x){
            return x ** 3;
        },
        rms(x,y){
            return Math.sqrt(0.5* (x**2 + y**2));
        }
    }
    window.math = math;
}) (window);


// Synchronous Syntax (CommonJS)
const math = {
    sqrt(x){
        return Math.sqrt(x);
    },
    square(x) {
        return x ** 2;
    },
    cube(x){
        return x ** 3;
    },
    rms(x,y){
        return Math.sqrt(0.5* (x**2 + y**2));
    }
}
module.exports.math = math;


// Asynchronous Syntax (RequireJS)
define (function (){
    const math = {
        sqrt(x){
            return Math.sqrt(x);
        },
        square(x) {
            return x ** 2;
        },
        cube(x){
            return x ** 3;
        },
        rms(x,y){
            return Math.sqrt(0.5* (x**2 + y**2));
        }
    }
    return math;
});


// Generic Syntax (UMD)
(
// Environment Detection
function(root, factory){
    if(typeof define === 'function' && define.amd){
        define([], factory);
    } else if (typeof exports === 'object'){
        module.exports = factory();
    } else {
        root.returnExports = factory();
    }
}
// Module Definition
(this, function(){
    const math = {
        sqrt(x){
            return Math.sqrt(x);
        },
        square(x) {
            return x ** 2;
        },
        cube(x){
            return x ** 3;
        },
        rms(x,y){
            return Math.sqrt(0.5* (x**2 + y**2));
        }
    }
    return math;
})
);


// Native Syntax
export const sqrt = Math.sqrt;
export function square(x){
    return x ** 2;
}
export function cube(x){
    return x ** 3;
}
export function rms(x,y){
    return Math.sqrt(0.5* (x**2 + y**2));
}
export default "No such element exists"
// index.js

// IIFE
// "math.js" should be imported as `<script src="./math.js"></script>`
console.log(math.sqrt(15),
            math.square(15),
            math.cube(15)); // 3.872983346207417 225 3375
console.log(math.rms(2, 3)); // 2.5495097567963922

// Synchronous Syntax (CommonJS)
const math = require("./math").math;
console.log(math.sqrt(15),
            math.square(15),
            math.cube(15)); // 3.872983346207417 225 3375
console.log(math.rms(2, 3)); // 2.5495097567963922

// Asynchronous Syntax (RequireJS)
require(["./math"], function(math){
    console.log(math.sqrt(15),
                math.square(15),
                math.cube(15)); // 3.872983346207417 225 3375
    console.log(math.rms(2, 3)); // 2.5495097567963922
});

// Native Syntax
import * from 'math';
console.log(math.sqrt(15),
            math.square(15),
            math.cube(15)); // 3.872983346207417 225 3375
console.log(math.rms(2, 3)); // 2.5495097567963922