en.javascript.info/1-js/6-objects-more/3-static-properties-and-methods/1-objects-counter/solution.md
Ilya Kantor 87bf53d076 update
2014-11-16 01:40:20 +03:00

29 lines
728 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Решение (как вариант):
```js
//+ 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, Последняя: (дата)
```