我这里有一些 JS 代码:
function Car(manufacturer, model, year) {
this.manufacturer = manufacturer;
this.model = model;
this.year = year == undefined ? new Date().getFullYear() : year;
this.getInfo = function(){
return this.manufacturer +' '+ this.model +' '+ this.year;
};
}
var bmw = new Car("BMW", "X5", 2010);
所以我想在控制台中有一些有趣的输出:
console.log('Car: ' + bmw); // Car: BMW X5 2010
如何在不调用任何方法的情况下做到这一点?
谢谢!
I need the 'getInfo' method, so I have simply changed my code:
function Car(manufacturer, model, year) {
this.manufacturer = manufacturer;
this.model = model;
this.year = year == undefined ? new Date().getFullYear() : year;
this.toString = this.getInfo = function(){
return this.manufacturer +' '+ this.model +' '+ this.year;
};
}