Mult in Javascript
Syntactically, JavaScript is fairly similar to the PHP and Perl implementations. However, this implementation of
Mult runs entirely on the client side.
Again, the multiplication routine is as simple as can be:
function mult( a, b )
{
var prod = a * b;
document.write( "<center>" );
document.write( "<h1><b>" );
document.write( a+" x "+b+" = "+prod );
document.write( "</b></h1>" );
document.write( "</center>" );
}
Likewise, the generation of the multiplication table is straightforward:
document.write( "<center>" );
document.write( "<table border=\"1\">" );
document.write( "<tr>" );
document.write( "<td>X</td>" );
for (var c=0; c<10; ++c) {
document.write( "<td>"+c+"</td>" );
}
document.write( "</tr>" );
for (var r=0; r<10; ++r) {
document.write( "<tr>" );
document.write( "<td>"+r+"</td>" );
for (var c=0; c<10; ++c) {
var link = alLink( "mult", r, c );
document.write( "<td> <a href=\""+link+"\">"+r+" x "+c+"</a></td>" );
}
document.write( "</tr>" );
}
document.write( "</table>" );
document.write( "</center>" );
The link is defined in more or less the same way as in PHP and Perl:
var link = alLink( "mult", r, c );
One difference with the JavaScript implementation is that, in
alinvoke.html, you must include the active link library after the definition of the
mult() routine:
<script language="javascript" src="activeLink.js"></script>
Otherwise, the
mult() routine will not be defined when
alExtractAndApply() is called.
Freezing and Thawing in JavaScript
To encode the function call in JavaScript, take a list containing the function named and arguments and call toSource() on it, which returns a piece of Javascript source code. Escape this code with the escape() function:
var enc = escape( info.toSource() );
This code, when evaluated, will return the original list:
var info = eval( unescape( results[1] ) );
alApply( info );
Next, apply the first element of the list to the rest of the elements of the list:
function alApply( info )
{
var fun = this[info[0]];
var funargs = new Array();
for (var i=1; i<info.length; ++i) {
funargs[i-1] = info[i];
}
fun.apply( null, funargs );