Skip to content

Commit 0fac5ec

Browse files
committed
secretos ninja js
1 parent 2c852e1 commit 0fac5ec

24 files changed

+553
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Listing 2.1</title>
5+
<script type="text/javascript" src="log.js"></script>
6+
<script type="text/javascript">
7+
var x = 213;
8+
log(x); //#1
9+
</script>
10+
</head>
11+
<body>
12+
</body>
13+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Listing 2.2</title>
5+
<script type="text/javascript" src="log.js"></script>
6+
<script type="text/javascript">
7+
var x = 213;
8+
log(x);
9+
</script>
10+
</head>
11+
<body>
12+
</body>
13+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script src="dist/jquery.js"></script>
2+
<script>
3+
$(document).ready(function() {
4+
$("#test").append("test");
5+
});
6+
</script>
7+
<style>
8+
#test { width: 100px; height: 100px; background: red; }
9+
</style>
10+
<div id="test"></div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<title>Test Suite</title>
4+
<script>
5+
6+
function assert(value, desc) {
7+
var li = document.createElement("li");
8+
li.className = value ? "pass" : "fail";
9+
li.appendChild(document.createTextNode(desc));
10+
document.getElementById("results").appendChild(li);
11+
}
12+
13+
window.onload = function() {
14+
assert(true, "The test suite is running.");
15+
assert(false, "Fail!");
16+
};
17+
</script>
18+
19+
<style>
20+
#results li.pass { color: green; }
21+
#results li.fail { color: red; }
22+
</style>
23+
</head>
24+
25+
<body>
26+
<ul id="results"></ul>
27+
</body>
28+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<html>
2+
<head>
3+
<title>Test Suite</title>
4+
<script>
5+
6+
(function() {
7+
var results;
8+
this.assert = function assert(value, desc) {
9+
var li = document.createElement("li");
10+
li.className = value ? "pass" : "fail";
11+
li.appendChild(document.createTextNode(desc));
12+
results.appendChild(li);
13+
if (!value) {
14+
li.parentNode.parentNode.className = "fail";
15+
}
16+
return li;
17+
};
18+
this.test = function test(name, fn) {
19+
results = document.getElementById("results");
20+
results = assert(true, name).appendChild(
21+
document.createElement("ul"));
22+
fn();
23+
};
24+
})();
25+
26+
window.onload = function() {
27+
test("A test.", function() {
28+
assert(true, "First assertion completed");
29+
assert(true, "Second assertion completed");
30+
assert(true, "Third assertion completed");
31+
});
32+
test("Another test.", function() {
33+
assert(true, "First test completed");
34+
assert(false, "Second test failed");
35+
assert(true, "Third assertion completed");
36+
});
37+
test("A third test.", function() {
38+
assert(null, "fail");
39+
assert(5, "pass")
40+
});
41+
};
42+
</script>
43+
<style>
44+
#results li.pass { color: green; }
45+
#results li.fail { color: red; }
46+
</style>
47+
</head>
48+
<body>
49+
<ul id="results"></ul>
50+
</body>
51+
</html>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<html>
2+
<head>
3+
<title>Test Suite</title>
4+
<script>
5+
(function() {
6+
var queue = [], paused = false, results;
7+
this.test = function(name, fn) {
8+
queue.push(function() {
9+
results = document.getElementById("results");
10+
results = assert(true, name).appendChild(
11+
document.createElement("ul"));
12+
fn();
13+
});
14+
runTest();
15+
};
16+
this.pause = function() {
17+
paused = true;
18+
};
19+
this.resume = function() {
20+
paused = false;
21+
setTimeout(runTest, 1);
22+
};
23+
function runTest() {
24+
if (!paused && queue.length) {
25+
(queue.shift())();
26+
if (!paused) {
27+
resume();
28+
}
29+
}
30+
}
31+
32+
this.assert = function assert(value, desc) {
33+
var li = document.createElement("li");
34+
li.className = value ? "pass" : "fail";
35+
li.appendChild(document.createTextNode(desc));
36+
results.appendChild(li);
37+
if (!value) {
38+
li.parentNode.parentNode.className = "fail";
39+
}
40+
return li;
41+
};
42+
})();
43+
window.onload = function() {
44+
test("Async Test #1", function() {
45+
pause();
46+
setTimeout(function() {
47+
assert(true, "First test completed");
48+
resume();
49+
}, 1000);
50+
});
51+
test("Async Test #2", function() {
52+
pause();
53+
setTimeout(function() {
54+
assert(true, "Second test completed");
55+
resume();
56+
}, 1000);
57+
});
58+
};
59+
</script>
60+
<style>
61+
#results li.pass {
62+
color: green;
63+
}
64+
65+
#results li.fail {
66+
color: red;
67+
}
68+
</style>
69+
</head>
70+
<body>
71+
<ul id="results"></ul>
72+
</body>
73+
</html>

secretos-ninja-js/chapter-2/log.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function log() {
2+
try {
3+
console.log.apply(console, arguments); //#1
4+
}
5+
catch(e) { //#2
6+
try {
7+
opera.postError.apply(opera, arguments); //#3
8+
}
9+
catch(e){
10+
alert(Array.prototype.join.call( arguments, " ")); //#4
11+
}
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Listing 2.1</title>
5+
<script type="text/javascript" src="log.js"></script>
6+
<script type="text/javascript">
7+
var x = 213;
8+
console.log(x);
9+
</script>
10+
</head>
11+
12+
<body>
13+
14+
</body>
15+
</html>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Listing 3.1</title>
5+
<script type="text/javascript" src="../scripts/assert.js"></script>
6+
<link href="../styles/assert.css" rel="stylesheet" type="text/css">
7+
</head>
8+
<body>
9+
<script type="text/javascript">
10+
11+
function isNimble(){ return true; } //#1
12+
13+
assert(typeof window.isNimble === "function", //#2
14+
"isNimble() defined");
15+
assert(isNimble.name === "isNimble",
16+
"isNimble() has a name");
17+
18+
19+
var canFly = function(){ return true; }; //#3
20+
21+
assert(typeof window.canFly === "function", //#4
22+
"canFly() defined");
23+
assert(canFly.name === "",
24+
"canFly() has no name");
25+
26+
27+
window.isDeadly = function(){ return true; }; //#5
28+
29+
assert(typeof window.isDeadly === "function", //#6
30+
"isDeadly() defined");
31+
32+
function outer(){ //#7
33+
assert(typeof inner === "function",
34+
"inner() in scope before declaration");
35+
function inner(){}
36+
assert(typeof inner === "function",
37+
"inner() in scope after declaration");
38+
assert(window.inner === undefined,
39+
"inner() not in global scope");
40+
}
41+
42+
outer(); //#8
43+
assert(window.inner === undefined,
44+
"inner() still not in global scope");
45+
46+
window.wieldsSword = function swingsSword() { return true; }; //#9
47+
48+
assert(window.wieldsSword.name === 'swingsSword',
49+
"wieldSword's real name is swingsSword");
50+
51+
</script>
52+
</body>
53+
</html>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Listing 3.2</title>
5+
<meta charset="utf-8">
6+
<script type="text/javascript" src="../scripts/assert.js"></script>
7+
<link href="../styles/assert.css" rel="stylesheet" type="text/css">
8+
</head>
9+
<body>
10+
<script type="text/javascript">
11+
12+
assert(true,"|----- BEFORE OUTER -----|"); //#1
13+
assert(typeof outer==='function',
14+
"outer() is in scope");
15+
assert(typeof inner==='function',
16+
"inner() is in scope");
17+
assert(typeof a==='number',
18+
"a is in scope");
19+
assert(typeof b==='number',
20+
"b is in scope");
21+
assert(typeof c==='number',
22+
"c is in scope");
23+
24+
function outer(){
25+
26+
assert(true,"|----- INSIDE OUTER, BEFORE a -----|"); //#2
27+
assert(typeof outer==='function',
28+
"outer() is in scope");
29+
assert(typeof inner==='function',
30+
"inner() is in scope");
31+
assert(typeof a==='number',
32+
"a is in scope");
33+
assert(typeof b==='number',
34+
"b is in scope");
35+
assert(typeof c==='number',
36+
"c is in scope");
37+
38+
var a = 1;
39+
40+
assert(true,"|----- INSIDE OUTER, AFTER a -----|"); //#3
41+
assert(typeof outer==='function',
42+
"outer() is in scope");
43+
assert(typeof inner==='function',
44+
"inner() is in scope");
45+
assert(typeof a==='number',
46+
"a is in scope");
47+
assert(typeof b==='number',
48+
"b is in scope");
49+
assert(typeof c==='number',
50+
"c is in scope");
51+
52+
function inner(){ /* does nothing */ }
53+
54+
var b = 2;
55+
56+
assert(true,"|----- INSIDE OUTER, AFTER inner() AND b -----|"); //#4
57+
assert(typeof outer==='function',
58+
"outer() is in scope");
59+
assert(typeof inner==='function',
60+
"inner() is in scope");
61+
assert(typeof a==='number',
62+
"a is in scope");
63+
assert(typeof b==='number',
64+
"b is in scope");
65+
assert(typeof c==='number',
66+
"c is in scope");
67+
68+
if (a == 1) {
69+
var c = 3;
70+
assert(true,"|----- INSIDE OUTER, INSIDE if -----|"); //#5
71+
assert(typeof outer==='function',
72+
"outer() is in scope");
73+
assert(typeof inner==='function',
74+
"inner() is in scope");
75+
assert(typeof a==='number',
76+
"a is in scope");
77+
assert(typeof b==='number',
78+
"b is in scope");
79+
assert(typeof c==='number',
80+
"c is in scope");
81+
}
82+
83+
assert(true,"|----- INSIDE OUTER, AFTER c -----|"); //#6
84+
assert(typeof outer==='function',
85+
"outer() is in scope");
86+
assert(typeof inner==='function',
87+
"inner() is in scope");
88+
assert(typeof a==='number',
89+
"a is in scope");
90+
assert(typeof b==='number',
91+
"b is in scope");
92+
assert(typeof c==='number',
93+
"c is in scope");
94+
95+
}
96+
97+
outer();
98+
99+
assert(true,"|----- AFTER OUTER -----|"); //#7
100+
assert(typeof outer==='function',
101+
"outer() is in scope");
102+
assert(typeof inner==='function',
103+
"inner() is in scope");
104+
assert(typeof a==='number',
105+
"a is in scope");
106+
assert(typeof b==='number',
107+
"b is in scope");
108+
assert(typeof c==='number',
109+
"c is in scope");
110+
111+
</script>
112+
</body>
113+
</html>

0 commit comments

Comments
 (0)