forked from liammclennan/JavaScript-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_about_functions_and_closure.js
More file actions
86 lines (74 loc) · 2.88 KB
/
12_about_functions_and_closure.js
File metadata and controls
86 lines (74 loc) · 2.88 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
describe("About Functions And Closure (topics/12_about_functions_and_closure.js)", function () {
it("defining functions directly", function () {
let result = "a";
function changeResult() {
// the ability to access a letiables defined in the same scope as the function is known as 'closure'
result = "b";
}
changeResult();
expect(__).toBe(result, "what is the value of result?");
});
it("assigning functions to letiables", function () {
const triple = function (input) {
return input * 3;
};
expect(__).toBe(triple(4), "what is triple 4?");
});
it("self invoking functions", function () {
let publicValue = "shared";
// self invoking functions are used to provide scoping and to alias letiables
(function (pv) {
let secretValue = "password";
expect(__).toBe(pv, "what is the value of pv?");
expect("__").toBe(
typeof secretValue,
"is secretValue available in this context?"
);
expect("__").toBe(
typeof publicValue,
"is publicValue available in this context?"
);
})(publicValue);
expect("__").toBe(
typeof secretValue,
"is secretValue available in this context?"
);
expect("__").toBe(
typeof publicValue,
"is publicValue available in this context?"
);
});
it("arguments array", function () {
let add = function () {
let total = 0;
for (let i = 0; i < arguments.length; i++) {
// complete the implementation of this method so that it returns the sum of its arguments
// __
}
// __
};
expect(15).toEqual(add(1, 2, 3, 4, 5), "add 1,2,3,4,5");
expect(9).toEqual(add(4, 7, -2), "add 4,7,-2");
});
it("using call to invoke function", function () {
let invokee = function (message) {
return this + message;
};
//another way to invoke a function is to use the call function which allows
//you to set the callers "this" context. Call can take any number of arguments:
//the first one is always the context that this should be set to in the called
//function, and the arguments to be sent to the function,multiple arguments are separated by commas.
let result = invokee.call("I am this!", "Where did it come from?");
expect(__).toBe(result, "what will the value of invokee's this be?");
});
it("using apply to invoke function", function () {
let invokee = function (message1, message2) {
return this + message1 + message2;
};
//similar to the call function is the apply function. Apply only has two
//arguments: the first is the context that this should be set to in the called
//function and the second is the array of arguments to be passed into the called function.
let result = invokee.apply("I am this!", ["I am arg1", "I am arg2"]);
expect(__).toBe(result, "what will the value of invokee's this be?");
});
});