Skip to content

Commit 50e31fc

Browse files
added tests for call and apply
1 parent a1900a6 commit 50e31fc

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

testFunctions.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2+
"http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
<head>
5+
<script src="support/jquery-1.4.1.js"></script>
6+
<link rel="stylesheet" href="support/qunit.css" type="text/css" media="screen" />
7+
<script type="text/javascript" src="support/qunit.js"></script>
8+
<script type="text/javascript" src="support/koans.js"></script>
9+
<script type="text/javascript" src="topics/about_functions_and_closure.js"></script>
10+
11+
</head>
12+
<body>
13+
<h1 id="qunit-header">QUnit example</h1>
14+
<h2 id="qunit-banner"></h2>
15+
<h2 id="qunit-userAgent"></h2>
16+
<h3 class="welcome_message">To begin, find the file 'topics/about_asserts.js', and complete the tests.</h3>
17+
<ol id="qunit-tests"></ol>
18+
<div id="qunit-fixture">test markup, will be hidden</div>
19+
</body>
20+
</html>

topics/about_asserts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module("About Asserts (topics/about_asserts.js)");
33

44
test("ok", function() {
5-
ok(false, 'what will satisfy the ok assertion?');
5+
ok(__, 'what will satisfy the ok assertion?');
66
});
77

88
test("not", function() {

topics/about_functions_and_closure.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)