up
This commit is contained in:
parent
4ae129054e
commit
ab9ab64bd5
476 changed files with 3370 additions and 532 deletions
|
@ -0,0 +1,24 @@
|
|||
|
||||
Surely it will work just fine.
|
||||
|
||||
Both nested functions are created within the same outer Lexical Environment, so they share access to the same `count` variable:
|
||||
|
||||
```js run
|
||||
function Counter() {
|
||||
let count = 0;
|
||||
|
||||
this.up = function() {
|
||||
return ++count;
|
||||
};
|
||||
|
||||
this.down = function() {
|
||||
return --count;
|
||||
};
|
||||
}
|
||||
|
||||
let counter = new Counter();
|
||||
|
||||
alert( counter.up() ); // 1
|
||||
alert( counter.up() ); // 2
|
||||
alert( counter.down() ); // 1
|
||||
```
|
|
@ -0,0 +1,29 @@
|
|||
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() ); // ?
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue