johnny-dreamguns
10/7/2019 - 9:39 AM

Decorator - Simple

// DECORATOR SIMPLE

// Basic decorator, add properties and methods to a Task instance

const Task = function(data) {
  this.name = data.name;
  this.completed = false;
}

Task.prototype.complete = function() {
  console.log('Completing ' + this.name);
  this.completed = true;
}

Task.prototype.save = function() {
  console.log('Saving ' + this.name);
}

const myTask = new Task({ name: 'Task' });
myTask.complete();
myTask.save();

const urgentTask = new Task({ name: 'Urgent' });
urgentTask.priority = 2;
urgentTask.notify = () => {
  console.log('notifying people');
}

urgentTask.save = function() {
  this.notify();
  Task.prototype.save.call(this);
}

urgentTask.complete();
urgentTask.save();