en.javascript.info/1-js/6-objects-more/5-static-properties-and-methods/1-objects-counter/solution.md
2015-01-14 10:23:45 +03:00

728 B
Raw Blame History

Решение (как вариант):

//+ run
function Article() {  
  this.created = new Date;

*!*
  Article.count++; // увеличиваем счетчик при каждом вызове
  Article.last = this.created; // и запоминаем дату
*/!*
}
Article.count = 0; // начальное значение 
// (нельзя оставить undefined, т.к. Article.count++ будет NaN)

Article.showStats = function() {
  alert('Всего: ' + this.count + ', Последняя: ' + this.last);
};

new Article();
new Article();

Article.showStats(); // Всего: 2, Последняя: (дата)

new Article();

Article.showStats(); // Всего: 3, Последняя: (дата)