我目前正在学习如何使用工厂模式创建对象'在Javascript中。我写了这段代码。我想通过调用vehicle.getInfo();
函数来调用getVehicle();
函数。
function getVehicle (theYear, theMake, theModel) {
var vehicle = new Object();
vehicle.year = theYear
vehicle.make = theMake
vehicle.model = theModel
vehicle.getInfo = function () {
return 'Vehicle: ' + this.year + ' ' + this.make + ' ' + this.model;
console.log('Vehicle: ' + this.year + ' ' + this.make + ' ' + this.model);
};
console.log(vehicle); // at this level, is there a way to call getInfo function without the actual "console.log(vehicle.getInfo());"
};
getVehicle("2014", "Tata", "Zest");
在getVehicle()
函数中,我想要console.log vehicle.getInfo()
方法返回的内容。但是我想这样做,而不使用console.log(vehicle.getInfo());
。
这可能吗?
你能帮帮我吗?