For the More Curious: Private Module Data
Inside a module, your constructors and
prototype methods have access to any variables
declared inside the IIFE. As an alternative to adding properties
to the prototype
, this is a way to share data
between instances but make it hidden from any code outside the
module. It looks like this:
(function (window) { 'use strict'; var App = window.App || {}; var launchCount = 0; function Spaceship() { // Initialization code goes here } Spaceship.prototype.blastoff = function () { // Closure scope allows access to the launchCount variable launchCount++; console.log('Spaceship launched!') } Spaceship.prototype.reportLaunchCount = function () { console.log('Total number of launches: ' + launchCount); } App.Spaceship ...
Get Front-End Web Development: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.