Skip to content

Instantly share code, notes, and snippets.

@marcofranssen
Created January 23, 2012 18:48
Show Gist options
  • Save marcofranssen/1664843 to your computer and use it in GitHub Desktop.
Save marcofranssen/1664843 to your computer and use it in GitHub Desktop.
Gist related to my "Writing modular JavaScript without polluting the global namespace" blogpost on http://marcofranssen.nl
; (function(jsShop, window, document, undefined) {
var product = jsShop.product = jsShop.product || (function() {
//an implementation like shoppingCart
}());
}(window._jsShop = window._jsShop || {}, window, document));
; (function(jsShop, window, document, undefined) {
var product = jsShop.product;
var shoppingCart = jsShop.shoppingCart = jsShop.shoppingCart || (function() {
var items = [];
var priceTotal = 0;
var addProduct = function(product) {
items.push(product);
updatePriceTotal();
};
var removeProduct = function(product) {
//remove product from items
updatePriceTotal();
};
var updatePriceTotal = function() {
//logic to update the priceTotal
//use public functions on product to get the price of products
};
return {
addProduct: addProduct,
removeProduct: removeProduct
}
}());
}(window._jsShop = window._jsShop || {}, window, document));
var product = (function() {
//an implementation like shoppingCart
}());
var shoppingCart = (function() {
var items = [];
var priceTotal = 0;
var addProduct = function(product) {
items.push(product);
updatePriceTotal();
};
var removeProduct = function(product) {
//remove product from items
updatePriceTotal();
};
var updatePriceTotal = function() {
//logic to update the priceTotal
//use public functions on product to get the price of products
};
return {
addProduct: addProduct,
removeProduct: removeProduct
}
}());
; (function($, jsShop, window, document, undefined) {
var yourModule = jsShop.yourModule = jsShop.yourModule || (function(){
//your module code
$('#shoppingCatTotalPrice').html('€ 8,75');
}());
}(jQuery, window._jsShop = window._jsShop || {}, window, document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment