11module ( "About Functions And Closure (topics/about_functions_and_closure.js)" ) ;
22
3- test ( "defining functions directly" , function ( ) {
3+ test ( "defining functions directly" , function ( ) {
44 var result = "a" ;
5+
56 function changeResult ( ) {
67 // the ability to access a variables defined in the same scope as the function is known as 'closure'
78 result = "b" ;
89 } ;
910 changeResult ( ) ;
10- equal ( __ , result , 'what is the value of result?' ) ;
11+ equal ( "b" , result , 'what is the value of result?' ) ;
1112} ) ;
1213
13- test ( "assigning functions to variables" , function ( ) {
14- var triple = function ( input ) {
14+ test ( "assigning functions to variables" , function ( ) {
15+ var triple = function ( input ) {
1516 return input * 3 ;
1617 } ;
17- equal ( __ , triple ( 4 ) , 'what is triple 4?' ) ;
18+ equal ( "12" , triple ( 4 ) , 'what is triple 4?' ) ;
1819} ) ;
1920
20- test ( "self invoking functions" , function ( ) {
21+ test ( "self invoking functions" , function ( ) {
2122 var publicValue = "shared" ;
2223
2324 // self invoking functions are used to provide scoping and to alias variables
24- ( function ( pv ) {
25+ ( function ( pv ) {
2526 var secretValue = "password" ;
26- equal ( __ , pv , 'what is the value of pv?' ) ;
27- equal ( "__ " , typeof ( secretValue ) , "is secretValue available in this context?" ) ;
28- equal ( "__ " , typeof ( publicValue ) , "is publicValue available in this context?" ) ;
27+ equal ( "shared" , pv , 'what is the value of pv?' ) ;
28+ equal ( "string " , typeof ( secretValue ) , "is secretValue available in this context?" ) ;
29+ equal ( "string " , typeof ( publicValue ) , "is publicValue available in this context?" ) ;
2930 } ) ( publicValue ) ;
3031
31- equal ( "__ " , typeof ( secretValue ) , "is secretValue available in this context?" ) ;
32- equal ( "__ " , typeof ( publicValue ) , "is publicValue available in this context?" ) ;
32+ equal ( "undefined " , typeof ( secretValue ) , "is secretValue available in this context?" ) ;
33+ equal ( "string " , typeof ( publicValue ) , "is publicValue available in this context?" ) ;
3334} ) ;
3435
35- test ( "arguments array" , function ( ) {
36- var add = function ( ) {
36+ test ( "arguments array" , function ( ) {
37+ var add = function ( ) {
3738 var total = 0 ;
38- for ( var i = 0 ; i < arguments . length ; i ++ ) {
39+ for ( var i = 0 ; i < arguments . length ; i ++ ) {
3940 // complete the implementation of this method so that it returns the sum of its arguments
4041 // __
4142 }
4243 // __
4344 } ;
4445
45- equal ( 15 , add ( 1 , 2 , 3 , 4 , 5 ) , "add 1,2,3,4,5" ) ;
46- equal ( 9 , add ( 4 , 7 , - 2 ) , "add 4,7,-2" ) ;
46+ equal ( 15 , add ( 1 , 2 , 3 , 4 , 5 ) , "add 1,2,3,4,5" ) ;
47+ equal ( 9 , add ( 4 , 7 , - 2 ) , "add 4,7,-2" ) ;
4748} ) ;
4849
49- test ( "using call to invoke function" , function ( ) {
50- var invokee = function ( message ) {
51- return this + message ;
50+ test ( "using call to invoke function" , function ( ) {
51+ var invokee = function ( message ) {
52+ return this + message ;
5253 } ;
53-
54+
5455 //another way to invoke a function is to use the call function which allows
5556 //you to set the callers "this" context. Call can take any number of arguments:
5657 //the first one is always the context that this should be set to in the called
5758 //function, and the arguments to be sent to the function,multiple arguments are separated by commas.
5859 var result = invokee . call ( "I am this!" , "Where did it come from?" ) ;
59-
60+
6061 equal ( __ , result , "what will the value of invokee's this be?" ) ;
6162} ) ;
6263
63- test ( "using apply to invoke function" , function ( ) {
64- var invokee = function ( message1 , message2 ) {
65- return this + message1 + message2 ;
64+ test ( "using apply to invoke function" , function ( ) {
65+ var invokee = function ( message1 , message2 ) {
66+ return this + message1 + message2 ;
6667 } ;
67-
68+
6869 //similar to the call function is the apply function. Apply only has two
6970 //arguments: the first is the context that this should be set to in the called
7071 //function and the second is the array of arguments to be passed into the called function.
71- var result = invokee . apply ( "I am this!" , [ "I am arg1" , "I am arg2" ] ) ;
72-
73- equal ( __ , result , "what will the value of invokee's this be?" ) ;
74- } ) ;
72+ var result = invokee . apply ( "I am this!" , [ "I am arg1" , "I am arg2" ] ) ;
7573
74+ equal ( __ , result , "what will the value of invokee's this be?" ) ;
75+ } ) ;
0 commit comments