ここにいくつかの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;
};
}