forked from liammclennan/JavaScript-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_functions_and_closure.js
More file actions
75 lines (60 loc) · 2.72 KB
/
about_functions_and_closure.js
File metadata and controls
75 lines (60 loc) · 2.72 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
module("About Functions And Closure (topics/about_functions_and_closure.js)");
test("defining functions directly", function() {
var result = "a";
function changeResult() {
// the ability to access a variables defined in the same scope as the function is known as 'closure'
result = "b";
};
changeResult();
equals(result, __, 'what is the value of result?');
});
test("assigning functions to variables", function() {
var triple = function(input) {
return input * 3;
};
equals(triple(4), __, 'what is triple 4?');
});
test("self invoking functions", function() {
var publicValue = "shared";
// self invoking functions are used to provide scoping and to alias variables
(function(pv) {
var secretValue = "password";
equals(pv, __, 'what is the value of pv?');
equals(typeof(secretValue), "__", "is secret value available in this context?");
equals(typeof(pv), "__", "is public value available in this context?");
})(publicValue);
equals(typeof(secretValue), "__", "is secret value available in this context?");
equals(typeof(publicValue), "__", "is public value available in this context?");
});
test("arguments array", function() {
var add = function() {
var total = 0;
for(var i = 0; i < arguments.length; i++) {
// complete the implementation of this method so that it returns the sum of its arguments
}
// __
};
equals(add(1,2,3,4,5), 15, "add 1,2,3,4,5");
equals(add(4,7,-2), 9, "add 1,2,3,4,5");
});
test("using call to invoke function",function(){
var 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.
var result = invokee.call("I am this!", "Where did it come from?");
equals(result,__,"what will the value of invokee's this be?");
});
test("using apply to invoke function",function(){
var 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 and array of arguments to be passed into the called function.
var result = invokee.apply("I am this!", ["I am arg1","I am arg2"]);
equals(result,__,"what will the value of invokee's this be?");
});