Module System

Function Declarations and Expressions

  • The function name can be omitted in function expressions
  • This creates an anonymous function
  • Typically, a function expression is used as an IIFE
  • As a result, function expressions run as soon as its defined
  • Consequently, they aren't hoisted to the global scope

Illustrating Declarations and Expressions

// function declaration
function multiply(num1, num2) {
    return num1 * num2;
}

// function expression
let multiply = function(num1, num2) {
    return num1 * num2;
}

Illustrating Function Hoisting

hoisted(); // logs "foo"
notHoisted(); // TypeError: notHoisted not a function

function hoisted() {
    console.log('foo');
}

var notHoisted = function() {
    console.log('bar');
};

Sample Structure of Modules

index.html
main.js
modules/
    canvas.js  // module
    square.js  // module

References

Previous
Next

Currying

Event Loop