Last active
September 27, 2016 17:28
-
-
Save jongrover/77be47b11fff7fa11b1cd5460120f939 to your computer and use it in GitHub Desktop.
Comparing Functional and Object Oriented patterns in JavaScript
This file contains 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 tasks = []; | |
function display() { | |
var output = ""; | |
for(i=0; i < tasks.length; i++) { | |
output += (i+1) + ". " + tasks[i] + "\n"; | |
} | |
alert(output); | |
} | |
function get() { | |
var taskDescription = prompt("Type your task"); | |
tasks.push(taskDescription); | |
display(); | |
} | |
get(); // get task 1 | |
get(); // get task 2 |
This file contains 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 taskModule = (function () { | |
var tasks = []; | |
function displayTask() { | |
var output = ""; | |
for(i=0; i < tasks.length; i++) { | |
output += (i+1) + ". " + tasks[i] + "\n"; | |
} | |
alert(output); | |
} | |
function getTask() { | |
var taskDescription = prompt("Type your task"); | |
tasks.push(taskDescription); | |
displayTask(); | |
} | |
return { | |
get: getTask | |
}; | |
})(); | |
taskModule.get(); // get task 1 | |
taskModule.get(); // get task 2 |
This file contains 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
function Task() { | |
this.constructor.all.push(this); | |
} | |
Task.all = []; | |
Task.prototype.display = function () { | |
var output = ""; | |
for(i=0; i < Task.all.length; i++) { | |
output += (i+1) + ". " + Task.all[i].description + "\n"; | |
} | |
alert(output); | |
}; | |
Task.prototype.get = function () { | |
var taskDescription = prompt("Type your task"); | |
this.description = taskDescription; | |
this.display(); | |
}; | |
var task1 = new Task(); | |
task1.get(); // get task 1 | |
var task2 = new Task(); | |
task2.get(); // get task 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment