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

Decorator - Advanced

// DECORATOR ADVANCED

// Create a whole new object that inherits from Task and adds and modifies methods

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 task = new Task({ name: 'basic' });
task.complete();
task.save();



const UrgentTask = function(name, priority) {
  Task.call(this, name);
  this.priority = priority;
}
UrgentTask.prototype = Object.create(Task.prototype);

UrgentTask.prototype.save = function() {
  console.log('do something special');
  Task.prototype.save.call(this);
}

const ut = new UrgentTask({ name: 'urgent' }, 2);
ut.save();
ut.complete();