Eloquent JavaScript 4
by Joe LeMonnier
JavaScript
function unless(test, then) {
if (!test) then();
}
function repeat(times, body) {
for (var i = 0; i < times; i++) body(i);
}
repeat(5, function(n) {
unless(n % 2, function() {
document.write(n, "is even<br>");
});
});
// → 0 is even
// → 2 is even
document.write('<br><br><br>');
g=function noisy(f) {
return function(arg) {
document.write("calling with", arg ,'<br> f= ', f);
var val = f(arg);
document.write("<br>called with", arg, "- got", val);
return val;
};
}
g(Boolean)(0);
//noisy(Boolean)(0);
// → calling with 0
// → called with 0 - got false
document.write('<br> Boolean(20) =      ' , Boolean(10));
function transparentWrapping(f) {
return function() {
return f.apply(null,arguments);
};
}
document.write('<br>');
s=transparentWrapping(Boolean);
document.write(s(1,0,1,0));
var thing={name:'Danny', born: 2001 }
var string = JSON.stringify({name: "X", born: 1980});
document.write('<br>String=' +string);
document.write('<br>Thing=' +thing);
// → {"name":"X","born":1980}
document.write('<br>string='+JSON.parse(string).born);
document.write('<br>thing =' +thing.born)
// → 1980