嘿伙计们,我正在攻读微软认证考试40-780和书中
培训指南:使用Javascript和CSS3在HTML5中编程
他们展示了如何在Javascript中实现继承的示例。我需要对这行代码进行上瘾的解释:
parent.call(this, year, make, model);
这种方法"如何调用"实际上在代码的上下文中工作。 这是我上面提到的书中的代码示例。
var Vehicle = (function () {
function Vehicle(year, make, model) {
this.year = year;
this.make = make;
this.model = model;
}
Vehicle.prototype.getInfo = function () {
return this.year + ' ' + this.make + ' ' + this.model;
};
Vehicle.prototype.startEngine = function () {
return 'Vroom';
};
return Vehicle;
})();
var Car = (function (parent) {
function Car(year, make, model) {
parent.call(this, year, make, model); // <-- how this works?
this.wheelQuantity = 4;
}
return Car;
})(Vehicle);
答案 0 :(得分:2)
汽车类扩展汽车类和所有&#34;此。&#34;在Vehicle中成为Car实例。因此,如果你构造一个新的Car对象,结果会变成这样,
var c = new Car(2020,"EU","GSX");
Car {year: 2020, make: "EU", model: "GSX", wheelQuantity: 4}
调用方法here
有解释