性能明智什麼是更好的做法: 要創建一個原型或將metood添加到構造器。更好的性能:對象原型還是構造函數原生函數?
這是我的代碼:
function DateGreeting(selector) {
this.element = document.querySelectorAll(selector)[0];
this.date = new Date();
this.hours = this.date.getHours();
this.greeting = function() {
if(this.hours <= 11) {
return "Morning";
} else if (this.hours >= 12 && this.hours <= 17) {
return "Afternoon";
} else {
return "Evening";
}
}
}
DateGreeting.prototype.append = function() {
this.element.innerHTML = this.greeting();
}
我也可以把this.greeting
爲原型,但將這種提高性能或降低呢? (或者什麼也不做)
我應該總是把方法放在原型或構造函數中嗎?
Look:http://stackoverflow.com/questions/12180790/defining-methods-vi一個原型-VS-使用,這個功能於的構造函數,真正-A-PERFO –