Created
March 1, 2014 01:28
-
-
Save dlion/9283412 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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