性能明智什么是更好的做法: 要创建一个原型或将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 –