Below is a transcript of a simple JavaScript shell session to give you a basic idea of the sort of things you can do with the interpreter. If you're familiar with webpage JavaScript, print
is the equivalent of document.write
. In the following example, the lines beginning with js>
represent input, while those starting with ==>
show the output.
[OS/2 window prompt]js -?
==> JavaScript-C 1.4 release 1 1998 10 31
==> usage: js [-w] [-v version] [-f scriptfile] [scriptfile] [scriptarg...]
-w
turns on reportWarnings
.
[OS/2 window prompt]>js
==> js>
js> help() <-- Note that you have to type "()
" after shell commands.
==> JavaScript-C 1.4 release 1 1998 10 31
==> Command Usage Description
==> ======= ===== ===========
==> version version [number] Get or set JavaScript version number
==> load load ['foo.js' ...] Load files named by string arguments
==> print print [expr ...] Evaluate and print expressions
==> help help [name ...] Display usage and help messages
==> quit quit Quit mocha
==> gc gc Run the garbage collector
==> trap trap [fun] [pc] expr Trap bytecode execution
==> untrap untrap [fun] [pc] Remove a trap
==> line2pc line2pc [fun] line Map line number to PC
==> pc2line pc2line [fun] [pc] Map PC to line number
==> dis dis [fun] Disassemble functions into bytecodes
==> dissrc dissrc [fun] Disassemble functions with source lines
==> notes notes [fun] Show source notes for functions
==> tracing tracing [toggle] Turn tracing on or off
==> stats stats [string ...] Dump 'arena', 'atom', 'global' stats
==> build build Show build date and time
js> str = "Hello Warpzilla"
==> Hello Warpzilla
js> print(str.bold())
==> <B>Hello Warpzilla</B>
js> print(str.fontcolor("indianred").italics())
==> <I><FONT COLOR="indianred">Hello Warpzilla</FONT></I>
js> strp = str.split('ll')
==> He,o Warpzi,a
js> sink = new Array(str, strp)
==> Hello Warpzilla,He,o Warpzi,a
js> sink[1]
==> He,o Warpzi,a
js> sink[1][1]
==> o Warpzi
js> str2 = sink[1]
==> He,o Warpzi,a
js> str3 = str2.join('k')
==> Heko Warpzika
js> function domath(a,b,c,d) { return (a + b) * (c / d) }
js> domath (2,3,4,5)
==> 4
domath()
into bytecodes:
js> dis(domath)
==> 00000: getarg 0
==> 00003: getarg 1
==> 00006: add
==> 00007: nop
==> 00008: getarg 2
==> 00011: getarg 3
==> 00014: div
==> 00015: nop
==> 00016: mul
==> 00017: return
==>
==> Source notes:
==> 0: 7 [ 7] paren
==> 1: 15 [ 8] xdelta
==> 2: 15 [ 0] paren
js> load("../samples/jsmath.js")
==> Yo Warpzilla!
==> Math.acos(-1) is 3.141592653589793
==> Math.cos(Math.PI/2) is 6.123031769111886e-17
==> Math.atan(1) is 0.7853981633974483
==> Math.atan(.5) is 0.4636476090008061
==> Math.atan2(90,15) is 1.4056476493802699
==> Math.atan2(15,90) is 0.16514867741462683
==> Math.exp(1) is 2.718281828459045
==> Math.sqrt(2) is 1.4142135623730951
==> Math.SQRT2 is 1.4142135623730951
yotest
function with source lines:
js> dissrc(yotest)
==> ;------------------------- 2: print("Yo Warpzilla!");
==> 00000: 2 name print
==> 00003: 2 pushobj
==> 00004: 2 string "Yo Warpzilla!"
==> 00007: 2 call 1
==> 00010: 2 popv
js>load("samples/jsfunc.js")
, run its switchtest
function:
js> switchtest("Bananas")
==> Bananas are $0.48 a pound.
js> switchtest("Oranges")
==> Oranges are $0.59 a pound.
js> switchtest("kiwi")
==> Sorry, we are out of kiwi.
js> nestedfunctest(7)
==> Nested function test:
==> 1 * 5 is 5
==> 2 * 5 is 10
==> 3 * 5 is 15
==> 4 * 5 is 20
==> 5 * 5 is 25
==> 6 * 5 is 30
Nested functions can be accessed with the dot operator. For instance, to call
function foo(huh)
which is nested inside function bar()
, use bar.foo(duh)
.
js> quit()
==> [OS/2 command prompt]
{
, pressing Enter takes you to the next line. The matching closing brace returns you to the JS prompt. Because you can't back up beyond the current line, writing lengthy functions at the prompt is not a very good idea.
js -f ../samples/jscal.js > jscal.html
and then have a look at jscal.html in your browser. You can't do that in the shell, however, because >
and |
are JavaScript operators.
- Henry Sobotka
98/12/05