-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclosures.js
More file actions
55 lines (42 loc) · 1.32 KB
/
closures.js
File metadata and controls
55 lines (42 loc) · 1.32 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function makeAdder(x) {
// parameter `x` is an inner variable
// inner function `add()` uses `x`, so
// it has a "closure" over it
function add(y) {
return y + x;
};
return add;
}
// `plusOne` gets a reference to the inner `add(..)`
// function with closure over the `x` parameter of
// the outer `makeAdder(..)`
var plusOne = makeAdder( 1 );
// `plusTen` gets a reference to the inner `add(..)`
// function with closure over the `x` parameter of
// the outer `makeAdder(..)`
var plusTen = makeAdder( 10 );
console.log(plusOne( 3 )); // 4 <-- 1 + 3
console.log(plusOne( 41 )); // 42 <-- 1 + 41
console.log(plusTen( 13 )); // 23 <-- 10 + 13
// more complicated example
// User() is referred to as a module
function User(){
var username, password;
function doLogin(user,pw) {
username = user;
password = pw;
// do the rest of the login work
}
var publicAPI = {
login: doLogin
};
return publicAPI;
}
// create a `User` module instance
var fred = User();
// you're referencing the publicAPI object that has been assigned
// to fred after you instantiate it with User()
// and the only object there is a local function
// which accepts the two parameters you pass it
fred.login( "fred", "12Battery34!" );
console.log("here's fred: " + fred.username); //undefined; no access