Javascript practice
by Adam Granger
HTML
<div id="console">
</div>
JavaScript
console.log = function(text) {
$("#console").append(text + "<br />");
}
Horse = function(name) {
this.name = name;
}
Horse.prototype.kill = function() {
console.log("killing " + this.name);
}
var a = new Horse("glue");
a.kill();
var b = new Horse("factory");
b.kill();
(function() {
console.log("Private scope oh yes");
})();
doIt(doIt);
doIt(function() {
console.log("Something else");
});
function doIt(anotherFn) {
console.log("doing " + anotherFn);
if (anotherFn == doIt) {
console.log("its the same function fool");
}
if (arguments.length > 0) {
anotherFn.call(arguments);
}
}
function factorial(x) {
return (function f(x) {
var returnValue;
if (x == 1) {
returnValue = x;
} else {
returnValue = f(x - 1) * x;
}
return returnValue;
})(x);
}
console.log("factorial 5 = " + factorial(5));