Advertisement
Guest User

JS Importer

a guest
Aug 16th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var require = (function(moduleMode)
  2. {
  3.     var _cache = {},
  4.         _ajax = new XMLHttpRequest();
  5.    
  6.     return function require(file)
  7.     {
  8.         if(!_cache[file])
  9.         {
  10.             try
  11.             {
  12.                 // async is set to false to make sure the browser waits for the content to be included.
  13.                 _ajax.open('GET', file, false);
  14.                 _ajax.send();
  15.            
  16.                 if(moduleMode)
  17.                 {
  18.                     // Make sure 'this' inside the module points towards a fresh object, not the window.
  19.                     _cache[file] = new Function(_ajax.responseText).call({});
  20.                 }
  21.                 else
  22.                 {
  23.                     // Make sure that the scope of the file is the window, not this place here.
  24.                     eval.call(window, _ajax.responseText);
  25.                     _cache[file] = true;
  26.                 }
  27.             }
  28.             catch(e)
  29.             {
  30.                 // Intercept any error to give a slighly more usefull message;
  31.                 throw(e.message + ' in file ' + file);
  32.             }
  33.         }
  34.        
  35.         return _cache[file];
  36.     }
  37. })(true);
  38.  
  39. var bar1 = require('js/bar.js');
  40. var bar2 = require('js/bar.js');
  41. var bar3 = require('js/bar.js'); // Thanks to caching, all 3 of these are references to the same object.
  42.  
  43. console.log(bar1.something);
  44. bar1.something = 'thing';
  45. console.log(bar2.something); // The module itself is a single instance, so values are shared.
  46.  
  47. var b1 = new bar1.Bar(); // New instances of objects inside the module
  48. var b2 = new bar2.Bar();
  49. b1.x(); // Prototypes work accross modules
  50. b2.x(); // Prototypes are still shared
  51.  
  52. //----- js/bar.js -----//
  53.  
  54. var foo = require('js/foo.js'); // Will include js/foo.js if it is not in the cache already.
  55.  
  56. var something = 'some';
  57.  
  58. function Bar()
  59. {
  60.     console.log(foo.param);
  61. }
  62.  
  63. // Why not put a module as prototype?
  64. Bar.prototype = foo;
  65.  
  66. // Carefully select what is publicly available.
  67. return {
  68.     Bar:Bar,
  69.     something:something
  70. }
  71.  
  72. //----- js/foo.js -----//
  73.  
  74. console.log('Imported!'); // Only fires once, thanks to caching.
  75.  
  76. // Private var, can not be accessed outside this module.
  77. var i = 1;
  78.  
  79. // 'this' refers to a new object {}, so it's save to use.
  80. this.x = function(){ console.log(i ++); };
  81.  
  82. this.param = 'value';
  83.  
  84. // Make this the module
  85. return this;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement