|
| 1 | +// a simple facade that masks the various browser-specific methods |
| 2 | +var window = window || null; |
| 3 | + |
| 4 | +function addEvent( element, event, callback ) { |
| 5 | + |
| 6 | + if( window.addEventListener ) { |
| 7 | + element.addEventListener( event, callback, false ); |
| 8 | + } else if( document.attachEvent ) { |
| 9 | + element.attachEvent( 'on' + event, callback ); |
| 10 | + } else { |
| 11 | + element[ 'on' + event ] = callback; |
| 12 | + } |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +// another example of a module which usages facade pattern to hide the direct access to |
| 17 | +// internal method |
| 18 | +var MyModule = ( function( window, undefined ) { |
| 19 | + |
| 20 | + |
| 21 | + function someMethod() { |
| 22 | + console.log( 'some method' ); |
| 23 | + } |
| 24 | + |
| 25 | + function someOtherMethod() { |
| 26 | + console.log( 'some other method' ); |
| 27 | + } |
| 28 | + |
| 29 | + // expose publicly available methods |
| 30 | + return { |
| 31 | + |
| 32 | + // in our normal revealing module pattern, we'd do the following: |
| 33 | + someMethod : someMethod, |
| 34 | + |
| 35 | + // in the facade pattern, we mask the internals so no one has direct access by doing this: |
| 36 | + someOtherMethodFacade : function() { |
| 37 | + someOtherMethod(); |
| 38 | + } |
| 39 | + |
| 40 | + }; |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +} )( window ); |
| 45 | + |
| 46 | +//example usages |
| 47 | + |
| 48 | +MyModule.someMethod(); // logs "some method" |
| 49 | +MyModule.someOtherMethodFacade(); //logs "some other method" |
| 50 | +MyModule.someOtherMethod(); // throw error "MyModule.someOtherMethod is not a function" |
0 commit comments