The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

The Module Pattern in Javascript (aka Immediately-Invoked Function Expression aka IIFE)

The Module Pattern in Javascript (aka Immediately-Invoked Function Expression aka IIFE)

5:05
Javascript modules are a design pattern that allow you to encapsulate your code into smaller self managing pieces. They help you separate your code, protect your variables, and provide an easy way to expose only the parts of your component that you want to be exposed.
Watch this lesson now
Avatar
egghead.io

Javascript modules are a design pattern that allow you to encapsulate your code into smaller self managing pieces. They help you separate your code, protect your variables, and provide an easy way to expose only the parts of your component that you want to be exposed.

Avatar
James

How do you approach the scenario where a module is dependant on another module or library? Does this pattern still work with that scenario?

Avatar
Jeremy

@James: This pattern works great for that scenario. You actually have two ways to receive dependencies.

If you have a static dependency:
//Have your module receive the dependency as a parameter
var calculator = (function (myDependencyAsAParameter){

}(myDependency);

If you have a dynamic dependency that may change at run time you can pass it in when you invoke the module just like how the example is doing with the onUpdateResult function.

In reply to James
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?