29 lines
417 B
Markdown
29 lines
417 B
Markdown
importance: 5
|
|
|
|
---
|
|
|
|
# Counter object
|
|
|
|
Here a counter object is made with the help of the constructor function.
|
|
|
|
Will it work? What will it show?
|
|
|
|
```js
|
|
function Counter() {
|
|
let count = 0;
|
|
|
|
this.up = function() {
|
|
return ++count;
|
|
};
|
|
this.down = function() {
|
|
return --count;
|
|
};
|
|
}
|
|
|
|
let counter = new Counter();
|
|
|
|
alert( counter.up() ); // ?
|
|
alert( counter.up() ); // ?
|
|
alert( counter.down() ); // ?
|
|
```
|
|
|