Created
August 14, 2011 09:57
-
-
Save addyosmani/1144758 to your computer and use it in GitHub Desktop.
Some examples for my large-scale JavaScript talk
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
| ////Module Pattern based on a singleton | |
| var basketModule = (function() { | |
| var basket = []; //private | |
| return { //exposed to public | |
| addItem: function(values) { | |
| basket.push(values); | |
| }, | |
| getItemCount: function() { | |
| return basket.length; | |
| }, | |
| getTotal: function(){ | |
| var q = this.getItemCount(),p=0; | |
| while(q--){ | |
| p+= basket[q].price; | |
| } | |
| return p; | |
| } | |
| } | |
| }()); | |
| //basketModule is an object with properties which can also be methods | |
| basketModule.addItem({item:'bread',price:0.5}); | |
| basketModule.addItem({item:'butter',price:0.3}); | |
| console.log(basketModule.getItemCount()); | |
| console.log(basketModule.getTotal()); | |
| //however, the following will not work: | |
| console.log(basketModule.basket);// (undefined as not inside the returned object) | |
| console.log(basket); //(only exists within the scope of the closure) | |
| ///// my example of a facade | |
| var module = (function() { | |
| var _private = { | |
| i:5, | |
| get : function() { | |
| console.log('current value:' + this.i); | |
| }, | |
| set : function( val ) { | |
| this.i = val; | |
| }, | |
| run : function() { | |
| console.log('running'); | |
| } | |
| }; | |
| return { | |
| facade : function( args ) { | |
| _private.set(args.val); | |
| _private.get(); | |
| if ( args.run ) { | |
| _private.run(); | |
| } | |
| } | |
| } | |
| }()); | |
| module.facade({run: true, val:10}); | |
| //outputs current value: 10, running | |
| ////// | |
| ////Pub/Sub inspired by @cedmax | |
| (function (_) { // _ is equal to the empty object passed as argument to the IIFE | |
| return { //public methods pub & sub: | |
| pub: function (a, b, c, d) { | |
| //pub takes 2 params: a string for the event name an optional object to be passed to the function subscribed | |
| for (d = -1, c = [].concat(_[a]); c[++d];) c[d](b) | |
| //loop on function registered on that event ed executing each one passing the optional object | |
| }, //won't break if no subscription on that event | |
| sub: function (a, b) { | |
| //sub takes 2 params: a string for the event name and | |
| //a function to be executed when the event is published | |
| (_[a] || (_[a] = [])).push(b) //push the function to the array | |
| //tied to that event (create the array if does not exist) | |
| } | |
| } | |
| })({}) | |
| //eventManager.sub("myEvent", function(a){alert(a.message)}); | |
| //eventManager.pub("myEvent", {message:'myEvent has been triggered'}); | |
| ////Mediator pattern (inspired by @rpflorence) | |
| /* | |
| Mediator | |
| -------- | |
| This object mediates custom events among objects on the page. Objects don't need to know about other objects in order to communicate with them--they simply subscribe to global publications. This decouples all of the objects from each other. In most cases, an object's entire implementation can be described in one code block anywhere in the application code. | |
| ### mediator method: publish | |
| Publishes an event to the application that objects can subscribe to, passing along information. | |
| mediator.publish('some-channel', arg, arg2); | |
| ### mediator method: installTo | |
| Extends an object with the `subscribe` and `publish` methods. | |
| mediator.installTo(obj); | |
| ### object method: subscribe | |
| Subscribes an object to a particular channel. When the mediator publishes the channel, this function is called, setting the context to the subscriber. | |
| var obj = { name: 'sam' }; | |
| obj.subscribe('some-channel', function(arg){ | |
| this.name = arg; | |
| }); | |
| ### object method: publish | |
| After installing mediator to the object, this method publishes an event to the application that objects can subscribe to, passing along information. | |
| mediator.installTo(obj); | |
| obj.publish('some-channel', arg, arg2); | |
| */ | |
| var mediator = (function(){ | |
| var subscribe = function(channel, fn){ | |
| if (!mediator.channels[channel]) mediator.channels[channel] = []; | |
| mediator.channels[channel].push({ context: this, callback: fn }); | |
| return this; | |
| }, | |
| publish = function(channel){ | |
| if (!mediator.channels[channel]) return false; | |
| var args = Array.prototype.slice.call(arguments, 1); | |
| for (var i = 0, l = mediator.channels[channel].length; i < l; i++) { | |
| var subscription = mediator.channels[channel][i]; | |
| subscription.callback.apply(subscription.context, args); | |
| } | |
| return this; | |
| }; | |
| return { | |
| channels: {}, | |
| publish: publish, | |
| subscribe: subscribe, | |
| installTo: function(obj){ | |
| obj.subscribe = subscribe; | |
| obj.publish = publish; | |
| } | |
| }; | |
| }()); | |
| mediator.installTo(Element.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment