-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathabout_functions_and_closure.js
More file actions
75 lines (61 loc) · 2.86 KB
/
about_functions_and_closure.js
File metadata and controls
75 lines (61 loc) · 2.86 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 variables defined in the same scope as the function is known as 'closure'
result = "b";
};
changeResult();
equal("b", result, 'what is the value of result?');
});
test("assigning functions to variables", function() {
var triple = function(input) {
return input * 3;
};
equal(12, 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";
equal("shared", pv, 'what is the value of pv?');
equal("string", typeof(secretValue), "is secretValue available in this context?");
equal("string", typeof(publicValue), "is publicValue available in this context?");
})(publicValue);
equal("undefined", typeof(secretValue), "is secretValue available in this context?");
equal("string", typeof(publicValue), "is publicValue 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
total = total + arguments[i];
}
return total;
};
equal(15, add(1,2,3,4,5), "add 1,2,3,4,5");
equal(9, add(4,7,-2), "add 4,7,-2");
});
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 caller's "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?");
equal("I am this!Where did it come from?", 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 the second is the array of arguments to be passed into the called function.
var result = invokee.apply("I am this!", ["I am arg1","I am arg2"]);
equal("I am this!I am arg1I am arg2", result, "what will the value of invokee's this be?");
});