Skip to content
Browse files

changed js-closures.md to show a closure

  • Loading branch information...
1 parent 035f9db commit c11eb8ff6cbace17f1fcb66d00225d199f1ecfd0 @MatForsberg MatForsberg committed
Showing with 10 additions and 8 deletions.
  1. +10 −8 js-closures.md
View
18 js-closures.md
@@ -1,14 +1,16 @@
Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created.
```js
-function init() {
- var name = "Mozilla"; // name is a local variable created by init
- function displayName() { // displayName() is the inner function, a closure
- alert(name); // use variable declared in the parent function
- }
- displayName();
-};
-init();
+function makeFunc() {
+ var name = "Mozilla";
+ function displayName() {
+ alert(name);
+ }
+ return displayName;
+}
+
+var myFunc = makeFunc();
+myFunc();
```
See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures)

0 comments on commit c11eb8f

Please sign in to comment.
Something went wrong with that request. Please try again.