|
1 | 1 |
|
2 | | -$(document).ready(function(){ |
3 | | - |
4 | | - module("About Functions And Closure (topics/about_functions_and_closure.js)"); |
5 | | - |
6 | | - test("defining functions directly", function() { |
7 | | - var result = "a"; |
8 | | - function changeResult() { |
9 | | - // the ability to access a variables defined in the same scope as the function is known as 'closure' |
10 | | - result = "b"; |
11 | | - }; |
12 | | - changeResult(); |
13 | | - equals(result, __, 'what is the value of result?'); |
14 | | - }); |
15 | | - |
16 | | - test("assigning functions to variables", function() { |
17 | | - var triple = function(input) { |
18 | | - return input * 3; |
19 | | - }; |
20 | | - equals(triple(4), __, 'what is triple 4?'); |
21 | | - }); |
22 | | - |
23 | | - test("self invoking functions", function() { |
24 | | - var publicValue = "shared"; |
25 | | - |
26 | | - // self invoking functions are used to provide scoping and to alias variables |
27 | | - (function(pv) { |
28 | | - var secretValue = "password"; |
29 | | - equals(pv, __, 'what is the value of pv?'); |
30 | | - equals(typeof(secretValue), "__", "is secret value available in this context?"); |
31 | | - equals(typeof(pv), "__", "is public value available in this context?"); |
32 | | - })(publicValue); |
33 | | - |
34 | | - equals(typeof(secretValue), "__", "is secret value available in this context?"); |
35 | | - equals(typeof(publicValue), "__", "is public value available in this context?"); |
36 | | - }); |
37 | | - |
38 | | - test("arguments array", function() { |
39 | | - var add = function() { |
40 | | - var total = 0; |
41 | | - for(var i = 0; i < arguments.length; i++) { |
42 | | - // complete the implementation of this method so that it returns the sum of its arguments |
43 | | - } |
44 | | - // __ |
45 | | - }; |
46 | | - |
47 | | - equals(add(1,2,3,4,5), 15, "add 1,2,3,4,5"); |
48 | | - equals(add(4,7,-2), 9, "add 1,2,3,4,5"); |
49 | | - }); |
| 2 | +module("About Functions And Closure (topics/about_functions_and_closure.js)"); |
50 | 3 |
|
| 4 | +test("defining functions directly", function() { |
| 5 | + var result = "a"; |
| 6 | + function changeResult() { |
| 7 | + // the ability to access a variables defined in the same scope as the function is known as 'closure' |
| 8 | + result = "b"; |
| 9 | + }; |
| 10 | + changeResult(); |
| 11 | + equals(result, __, 'what is the value of result?'); |
| 12 | +}); |
| 13 | + |
| 14 | +test("assigning functions to variables", function() { |
| 15 | + var triple = function(input) { |
| 16 | + return input * 3; |
| 17 | + }; |
| 18 | + equals(triple(4), __, 'what is triple 4?'); |
| 19 | +}); |
| 20 | + |
| 21 | +test("self invoking functions", function() { |
| 22 | + var publicValue = "shared"; |
| 23 | + |
| 24 | + // self invoking functions are used to provide scoping and to alias variables |
| 25 | + (function(pv) { |
| 26 | + var secretValue = "password"; |
| 27 | + equals(pv, __, 'what is the value of pv?'); |
| 28 | + equals(typeof(secretValue), "__", "is secret value available in this context?"); |
| 29 | + equals(typeof(pv), "__", "is public value available in this context?"); |
| 30 | + })(publicValue); |
| 31 | + |
| 32 | + equals(typeof(secretValue), "__", "is secret value available in this context?"); |
| 33 | + equals(typeof(publicValue), "__", "is public value available in this context?"); |
| 34 | +}); |
| 35 | + |
| 36 | +test("arguments array", function() { |
| 37 | + var add = function() { |
| 38 | + var total = 0; |
| 39 | + for(var i = 0; i < arguments.length; i++) { |
| 40 | + // complete the implementation of this method so that it returns the sum of its arguments |
| 41 | + } |
| 42 | + // __ |
| 43 | + }; |
| 44 | + |
| 45 | + equals(add(1,2,3,4,5), 15, "add 1,2,3,4,5"); |
| 46 | + equals(add(4,7,-2), 9, "add 1,2,3,4,5"); |
51 | 47 | }); |
0 commit comments