0
我是javascript的新用戶。我想通過使用print方法爲具有標題,作者和長度屬性的書寫一個構造函數。我嘗試了以下,我知道它不工作。請幫助!Thx!如何寫一個書寫構造函數並打印所有屬性而不使用Javascript中的CLASS函數?
這裏是我試圖代碼:
function Book(title,author,page){
this.title = title;
this.author = author;
this.page = page
this.toString = toString
}
function toString(){
return this.title + 'by' + this.author + ', is '+ this.page + ' long.'
}
print(){
console.log(this.toString());
}
var mobyDick = new Book ('Hamlet' , 'William Shakespeare' , 82);
Hamlet.print();
什麼是「哈姆雷特」應該指的是什麼?你爲什麼要'印刷'和'toSring'? 'console.log(mobyDick.toString())'會工作得很好。 –