Created
November 18, 2012 18:25
-
-
Save peb7268/4106655 to your computer and use it in GitHub Desktop.
JavaScript: Module Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** A Better way with the module pattern ******************************************************************************#/ | |
* Our goals: | |
* | |
* 1) We define a module and scope it within a self invoking anonymous function | |
* 2) We perform a global import (by passing jQuery in as a param) and namespace it so we circumvent naming collisions | |
* 3) We then export the module with the return keyword | |
************************************************************************************************************************/ | |
var MODULE = (function($){ | |
//private vars | |
var secretText ="shhh dont tell... im a secret", | |
myBankAccountNumber = 12345, | |
mod = {}; | |
//privateMethods | |
makeAWithdrawl = function(myBankAccountNumber){ | |
console.log('Your Bank account number is: ' + myBankAccountNumber + ' and youre broke chump'); | |
} | |
//Public methods | |
mod.getText = function(selector){ | |
console.log($(selector).text().trim()); | |
} | |
mod.setText = function(selector, message){ | |
console.log('setting text'); | |
$(selector).text(message); | |
} | |
mod.ripYouOff = function(myBankAccountNumber){ | |
console.log('Your Bank account number is: ' + myBankAccountNumber + ' and youre broke chump'); | |
} | |
return mod; | |
})(jQuery); | |
//And many lines later someone comes along and adds a new setText function | |
function setText(selector){ | |
$(selector).text('im broken now...boo'); | |
} | |
//Implementation | |
$(document).ready(function(){ | |
setText('#myDiv'); | |
MODULE.setText('#myDiv','I am some text to be set'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment