I was watching a javascript design patterns course and I came through this.
var Task = function(name) {
this.name = name;
this.completed = false;
}
Task.prototype.complete = function(){
console.log('completing task: ' + this.name)
this.completed = true;
}
Task.prototype.save = function(x){
console.log(`x is ${x}`);
console.log('saving task: ' + this.name);
}
// inheriting from Task could be done like this :
var UrgentTask = function(name, priority) {
// first you need to inherit the methods and variables
Task.call(this, name);
this.priority = priority;
}
// Second you need to inherit it's prototypes
// now what is the difference between this and just = Task.prototype ??
// UrgentTask.prototype = Object.create(Task.prototype);
UrgentTask.prototype = Task.prototype;
UrgentTask.prototype.notify = function(){
console.log('Just notifing ..');
}
UrgentTask.prototype.save = function() {
this.notify();
// Task.prototype.save.call(this,1);
}
// var ut = new UrgentTask('New Urgent Task', 1)
// ut.save();
var t = new Task('New Urgent Task')
t.save();
Why do changing in UrgentTask.prototype affects Task.prototype, I mean shouldn't the equality goes one way ?