Created
January 23, 2012 18:48
-
-
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
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
; (function(jsShop, window, document, undefined) { | |
var product = jsShop.product = jsShop.product || (function() { | |
//an implementation like shoppingCart | |
}()); | |
}(window._jsShop = window._jsShop || {}, window, document)); |
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
; (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)); |
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
var product = (function() { | |
//an implementation like shoppingCart | |
}()); |
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
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 | |
} | |
}()); |
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
; (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