Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. // PATTERN 1: DEFINE A GLOBAL
  2.  
  3. // foo.js
  4. foo = function () {
  5. console.log('foo!');
  6. }
  7.  
  8. // app.js
  9. require('./foo.js');
  10. foo();
  11.  
  12.  
  13. // PATTERN 2: EXPORT AN ANONYMOUS FUNCTION
  14.  
  15. // bar.js
  16. module.exports = function () {
  17. console.log('bar!');
  18. }
  19.  
  20. // app.js
  21. var bar = require('./bar.js');
  22. bar();
  23.  
  24.  
  25. // PATTERN 3: EXPORT A NAMED FUNCTION
  26.  
  27. // fiz.js
  28. exports.fiz = function () {
  29. console.log('fiz!');
  30. }
  31.  
  32. // app.js
  33. var fiz = require('./fiz.js').fiz;
  34. fiz();
  35.  
  36.  
  37. // PATTERN 4: EXPORT AN ANONYMOUS OBJECT
  38.  
  39. // buz.js
  40. var Buz = function () {};
  41.  
  42. Buz.prototype.log = function () {
  43. console.log('buz!');
  44. };
  45.  
  46. module.exports = new Buz();
  47.  
  48. // app.js
  49. var buz = require('./buz.js');
  50. buz.log();
  51.  
  52.  
  53. // PATTERN 5: EXPORT A NAMED OBJECT
  54.  
  55. // baz.js
  56. var Baz = function () {};
  57.  
  58. Baz.prototype.log = function () {
  59. console.log('baz!');
  60. };
  61.  
  62. exports.Baz = new Baz();
  63.  
  64. // app.js
  65. var baz = require('./baz.js').Baz;
  66. baz.log();
  67.  
  68.  
  69. // PATTERN 6: EXPORT AN ANONYMOUS PROTOTYPE
  70.  
  71. // doo.js
  72. var Doo = function () {};
  73.  
  74. Doo.prototype.log = function () {
  75. console.log('doo!');
  76. }
  77.  
  78. module.exports = Doo;
  79.  
  80. // app.js
  81. var Doo = require('./doo.js');
  82. var doo = new Doo();
  83. doo.log();
  84.  
  85.  
  86. // PATTERN 7: EXPORT A NAMED PROTOTYPE
  87.  
  88. // qux.js
  89. var Qux = function () {};
  90.  
  91. Qux.prototype.log = function () {
  92. console.log('baz!');
  93. };
  94.  
  95. exports.Qux = Qux;
  96.  
  97. // app.js
  98. var Qux = require('./qux.js').Qux;
  99. var qux = new Qux();
  100. qux.log();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement