@@ -45,3 +45,31 @@ test("arguments array", function() {
4545 equals ( add ( 1 , 2 , 3 , 4 , 5 ) , 15 , "add 1,2,3,4,5" ) ;
4646 equals ( add ( 4 , 7 , - 2 ) , 9 , "add 1,2,3,4,5" ) ;
4747} ) ;
48+
49+ test ( "using call to invoke function" , function ( ) {
50+ var invokee = function ( message ) {
51+ return this + message ;
52+ } ;
53+
54+ //another way to invoke a function is to use the call function which allows
55+ //you to set the callers "this" context. Call can take any number of arguments:
56+ //the first one is always the context that this should be set to in the called
57+ //function, and the arguments to be sent to the function,multiple arguments are separated by commas.
58+ var result = invokee . call ( "I am this!" , "Where did it come from?" ) ;
59+
60+ equals ( result , "I am this!Where did it come from?" , "what will the value of invokee's this be?" ) ;
61+ } ) ;
62+
63+ test ( "using apply to invoke function" , function ( ) {
64+ var invokee = function ( message1 , message2 ) {
65+ return this + message1 + message2 ;
66+ } ;
67+
68+ //similar to the call function is the apply function. Apply only has two
69+ //arguments: the first is the context that this should be set to in the called
70+ //function and and 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+ equals ( result , __ , "what will the value of invokee's this be?" ) ;
74+ } ) ;
75+
0 commit comments