Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. /*
  2. * Constructor Pattern
  3. * -------------------
  4. * Used to create a new object with its own scope.
  5. * You need a constructor function which when run with the new keyword will return
  6. * a new object.
  7. * The new keyword:
  8. * - Creates a new object
  9. * - Links the object to an Object prototype
  10. * - Binds this to the new object
  11. * - Returns this
  12. */
  13.  
  14. function Task(title,description) {
  15. this.title = title;
  16. this.description = description;
  17. this.completed = false;
  18. this.getTitle = function() {
  19. return this.title
  20. }
  21. this.completeTask = function() {
  22. this.completed = true;
  23. console.log(`${this.title} has been completed.😁`)
  24. }
  25. }
  26.  
  27. var buyApple = new Task('Buy Apples', 'Buy 5 Apples')
  28. console.log(buyApple)
  29.  
  30. /*
  31. * One caveat of the constructor above is that all its functions will be replicated
  32. * for each of the instantiated tasks. if we have 100 tasks we'll have 100 getTitle()s
  33. * We can fix it using prototypes.
  34. * className.prototype.methodName = sharedFunction() { ... }
  35. */
  36.  
  37. Task.prototype.getDetails = function () {
  38. return `${this.title} \n ${this.description}`
  39. } // All instantiated Tasks will link to 1 getDetails()
  40.  
  41.  
  42. /*
  43. * Module Pattern
  44. * --------------
  45. * Used to encapsulate a collection of similar functions/methods
  46. * Just an object with methods
  47. * If made using a function we can also have the ability to define private variables
  48. * that are only accessible from the methods.
  49. */
  50.  
  51. // Repo is acolection of functions handling interactions with the DB
  52. var repo = function () {
  53. // DB config & setup
  54. let db = { ... }
  55. return {
  56. get: function(id) {
  57. console.log(`Getting task ${id}`);
  58. return {
  59. name: 'new task from db'
  60. }
  61. },
  62. save: function(task) {
  63. console.log(`Saving ${task.name} to the db`)
  64. }
  65. }
  66. }
  67.  
  68. module.exports = repo()
  69.  
  70. // To use this module in another file...
  71. var Repo = require(`path\to\module`)
  72.  
  73. // We can then use the functions provided
  74. let taskFromRepo = Repo.get()
  75.  
  76. Task.prototype.savr = function () {
  77. console.log(`Saving task: ${this.name}`);
  78. Repo.save(this);
  79. }
  80.  
  81. Repo.save(taskFromRepo)
  82.  
  83.  
  84. /*
  85. * Factory Pattern
  86. * ---------------
  87. * Provides a common place for initializing objects
  88. */
  89.  
  90. var repoFactory = function() {
  91. var repos = this;
  92. var repoList = [
  93. {name: 'task', source: 'path\to\repo'},
  94. {name: 'project', source: 'path\to\repo'},
  95. {name: 'user', source: 'path\to\repo'},
  96. ]
  97. repoList.forEach(function(repo) {
  98. repos[repo.name] = require(repo.source)()
  99. });
  100. }
  101.  
  102. module.exports = new repoFactory;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement