Skip to content

Instantly share code, notes, and snippets.

@bernbecht
Last active March 18, 2016 01:23
Show Gist options
  • Save bernbecht/ab5889ddf145bc0332f8 to your computer and use it in GitHub Desktop.
Save bernbecht/ab5889ddf145bc0332f8 to your computer and use it in GitHub Desktop.
Decorator Design Pattern for JS
var Task = function (name) {
this.name = name;
this.completed = false;
//implicit return this here;
}
Task.prototype.save = function() {
console.log("saving...");
}
Task.prototype.complete = function() {
console.log("completing...");
}
var myTask = new Task("new task");
myTask.save();
myTask.complete();
var UrgentTask = function(name, priority) {
//Inits super
Task.call(this.name);
this.priority = priority;
}
//Creates a new prototype object for UrgentTask based on Task's
UrgentTask.prototype = Object.prototype(Task.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment