Created
November 6, 2017 06:52
-
-
Save YoneMoreno/f118e1e6c92ccce3beb1674f6f64bc33 to your computer and use it in GitHub Desktop.
Module Pattern learning design patterns https://app.pluralsight.com/player?course=javascript-practical-design-patterns&author=jonathan-mills&name=javascript-practical-design-patterns-m3&clip=9&mode=live
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
var Task = require('./task'); | |
var Repo = require('./taskRepository'); | |
var task1 = new Task(Repo.get(1)); | |
var task2 = new Task({name: 'create a demo for modules'}); | |
var task3 = new Task({name: 'create a demo for singletons'}); | |
var task4 = new Task({name: 'create a demo for prototypes'}); | |
task1.complete(); | |
task2.save(); | |
task3.save(); | |
task4.save(); |
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
var Repo = require('./taskRepository'); | |
var Task = function(data){ | |
this.name=data.name; | |
this.completed=false; | |
} | |
Task.prototype.complete = function () { | |
console.log('completing task: ' + this.name); | |
this.completed=true; | |
}; | |
Task.prototype.save=function(){ | |
console.log('saving Task: ' + this.name); | |
Repo.save(this); | |
}; | |
module.exports = Task; |
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
var repo = function(){ | |
var db = {}; | |
var get = function(id){ | |
console.log('Getting task ' + id); | |
return { | |
name: 'new task from db' | |
} | |
}; | |
var save = function(task){ | |
console.log('Saving ' + task.name + ' to the db'); | |
}; | |
return { | |
get: get, | |
save: save | |
} | |
} | |
module.exports = repo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment