Starting to read javascript ninja book and I really do not understand why the word 'this' is needed in below example. I tried it w/out it and code do not run. What purpose does the 'this' serve in below context? I think I understand the 'this'(or maybe not at all) but in below, I just do not understand. Please let me know! thank you.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
(function() {
var results;
this.assert = function assert(value,desc) {
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
results.appendChild(li);
if ( !value ) {
li.parentNode.parentNode.className = "fail";
}
return li;
};
//this.test = function test(name, fn) {
this.test = function test(name, fn) {
results = document.getElementById("results");
results = assert(true, name).appendChild(document.createElement("ul"));
fn();
};
})();
window.onload = function() {
test("A test", function() {
assert(true, "First assertion completed");
assert(true, "Second assertion completed");
assert(true, "Third assertion completed");
});
test("Another stupid test", function() {
assert(true, "First assertion completed");
assert(true, "Second assertion completed");
assert(true, "Third assertion completed");
});
test("A third test", function() {
assert(null, "fail");
assert(5, "pass")
});
};
</script>
<style type="text/css">
#results li.pass { color: green;}
#results li.fail { color: red;}
</style>
</head>
<body>
<ul id="results"</ul>
</body>
</html>