29 lines
538 B
Markdown
29 lines
538 B
Markdown
importance: 5
|
|
|
|
---
|
|
|
|
# Which variables are available?
|
|
|
|
The function `makeWorker` below makes another function and returns it. That new function can be called from somewhere else.
|
|
|
|
Will it have access to the outer variables from its creation place, or the invocation place, or both?
|
|
|
|
```js
|
|
function makeWorker() {
|
|
let name = "Pete";
|
|
|
|
return function() {
|
|
alert(name);
|
|
};
|
|
}
|
|
|
|
let name = "John";
|
|
|
|
// create a function
|
|
let work = makeWorker();
|
|
|
|
// call it
|
|
work(); // what will it show?
|
|
```
|
|
|
|
Which value it will show? "Pete" or "John"?
|