我正在观看关于JS设计模式的课程,其中一个例子中有这段代码:
var Task = function (name) {
this.name = name;
this.completed = false;
}
Task.prototype.save = function () {
console.log('saving Task: ' + this.name);
};
var UrgentTask = function (name, priority) {
Task.call(this, name);
this.priority = priority;
};
UrgentTask.prototype = Object.create(Task.prototype);
UrgentTask.prototype.save = function () {
console.log('do special stuff before saving');
//why use Task.prototype.save and not Task.save directly.
Task.prototype.save.call(this);
};
var ut = new UrgentTask('This is urgent', 1);
ut.save();
我的问题是为什么在UrgentTask.prototype.save函数中我必须使用.call()方法直接使用Task.prototype.save而不是Task.save? 为什么如果我使用Task.save它返回不能读取未定义的属性'call'??