forked from mrdavidlaing/javascript-koans
-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathAboutFunctions.js
113 lines (86 loc) · 2.88 KB
/
AboutFunctions.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
describe("About Functions", function() {
it("should declare functions", function() {
function add(a, b) {
return a + b;
}
expect(add(1, 2)).toBe(3);
});
it("should know internal variables override outer variables", function () {
var message = "Outer";
function getMessage() {
return message;
}
function overrideMessage() {
var message = "Inner";
return message;
}
expect(getMessage()).toBe("Outer");
expect(overrideMessage()).toBe("Inner");
expect(message).toBe("Outer");
});
it("should have lexical scoping", function () {
var variable = "top-level";
function parentfunction() {
var variable = "local";
function childfunction() {
return variable;
}
return childfunction();
}
expect(parentfunction()).toBe("local");
});
it("should use lexical scoping to synthesise functions", function () {
function makeIncreaseByFunction(increaseByAmount) {
return function (numberToIncrease) {
return numberToIncrease + increaseByAmount;
};
}
var increaseBy3 = makeIncreaseByFunction(3);
var increaseBy5 = makeIncreaseByFunction(5);
expect(increaseBy3(10) + increaseBy5(10)).toBe(28);
});
it("should allow extra function arguments", function () {
function returnFirstArg(firstArg) {
return firstArg;
}
expect(returnFirstArg("first", "second", "third")).toBe('first');
function returnSecondArg(firstArg, secondArg) {
return secondArg;
}
expect(returnSecondArg("only give first arg")).toBe(undefined);
function returnAllArgs() {
var argsArray = [];
for (var i = 0; i < arguments.length; i += 1) { // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/length
argsArray.push(arguments[i]);
}
return argsArray.join(",");
}
expect(returnAllArgs("first", "second", "third")).toBe("first,second,third");
});
it("should pass functions as values", function () {
var appendRules = function (name) {
return name + " rules!";
};
var appendDoubleRules = function (name) {
return name + " totally rules!";
};
var praiseSinger = { givePraise: appendRules };
expect(praiseSinger.givePraise("John")).toBe("John rules!");
praiseSinger.givePraise = appendDoubleRules;
expect(praiseSinger.givePraise("Mary")).toBe("Mary totally rules!");
});
it("should use function body as a string", function () {
var add = new Function("a", "b", "return a + b;");
expect(add(1, 2)).toBe(3);
var multiply = function (a, b) {
//An internal comment
return a * b;
};
expect(multiply.toString()).toBe(
'function (a, b) {
//An internal comment
return a * b;
}'
);
});
});