Skip to content

Instantly share code, notes, and snippets.

@jongrover
Last active September 27, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongrover/77be47b11fff7fa11b1cd5460120f939 to your computer and use it in GitHub Desktop.
Save jongrover/77be47b11fff7fa11b1cd5460120f939 to your computer and use it in GitHub Desktop.
Comparing Functional and Object Oriented patterns in JavaScript
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
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
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