Skip to content

Instantly share code, notes, and snippets.

@dlion
Created March 1, 2014 01:28
Show Gist options
  • Select an option

  • Save dlion/9283412 to your computer and use it in GitHub Desktop.

Select an option

Save dlion/9283412 to your computer and use it in GitHub Desktop.
var basketModule = (function () {
//Privates
var basket = [];
function doSomethingPrivate() {
//
}
function doSomethingElsePrivate() {
//...
}
//Return an object exposed to the public
return {
//Add items to our basket
addItem: function( values ) {
basket.push(values);
},
getItemCount: function () {
return basket.length;
},
//Public alias to a private function
doSomething: doSomethingPrivate,
// get the total value of items in the basket
getTotal: function () {
var q = this.getItemCount(),
p = 0;
while(q--) {
p += basket[q].price;
}
return p;
}
};
})();
basketModule.addItem({
item: "bread",
price: 0.5
});
basketModule.addItem({
item: "butter",
price: 0.3
});
console.log( basketModule.getItemCount() );
console.log( basketModule.getTotal() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment