forked from basarat/typescript-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosure.js
41 lines (41 loc) · 1.12 KB
/
closure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use strict";
function outerFunction(arg) {
var variableInOuterFunction = arg;
function bar() {
console.log(variableInOuterFunction);
}
bar();
}
outerFunction("hello closure");
var another;
(function (another) {
function outerFunction(arg) {
var variableInOuterFunction = arg;
return function () {
console.log(variableInOuterFunction);
};
}
var innerFunction = outerFunction("hello closure!");
innerFunction();
})(another = exports.another || (exports.another = {}));
var revealing;
(function (revealing) {
function createCounter() {
var val = 0;
return {
increment: function () { val++; },
getVal: function () { return val; }
};
}
var counter = createCounter();
counter.increment();
console.log(counter.getVal());
})(revealing = exports.revealing || (exports.revealing = {}));
var server;
(function (server) {
server.on(function handler(req, res) {
loadData(req.id).then(function (data) {
res.send(data);
});
});
})(server = exports.server || (exports.server = {}));