This page is the online technical companion to the VisiBone JavaScript Card and JavaScript Foldouts. Contributions and feedback are most welcome! stein@visibone.com
Contents:
Frequently Asked Questions
Errata
FAQ1: What is it with this assert() function?
FAQ2: How do I disable right-click to protect my images?
FAQ3: Can you help me find a complete online JavaScript reference?
FAQ1: (a) What is the assert() function? (b) Is that a built-in function? (c) Why are the JavaScript Card and Foldouts full of assert()'s?
See the assertiveness section in the Card and Foldouts.
(a) The assert() function is the core of unit testing, a great way to build in reliability. Each assert function is a little test. It's silent when code works, but reports when it doesn't. A very simple example:
Only if the expression results in is false does anything dramatic happen, maybe a popup warning.assert(2+2 == 4);Unit testing means religiously writing tests before coding a solution, then rerunning the tests often to find out early if something breaks. For more about the power of unit testing, and its contexts, see the Extreme Programming web site, www.xprogramming.com. I especially recommend Extreme Programming Installed by Ron Jeffries and others.
(b) The assert() function is the only function that is not built in. It will probably come standard one day but not yet. Today you probably have to write or install your own assert() function. The good news is it can be very easy. Here's a very simple example:
This failure message isn't very informative but there are lots of ways to improve that.function assert(fact) { if (!fact) { alert("Assert failure!"); } }Here are some simple asserts.js functions I've developed. Example usages:
assert("2+2 == 4");
assertEquals("2+2", "4");
assertNull("window.nosuchmember");
assertNotNull("window.document");Note you pass the code as strings.
For a very useful assert() and companion tools for Unit Testing, see Edward Hiett's JavaScript Unit Testing Framework JsUnit at www.jsunit.net. I used a modified version to test all the code in the Card and Foldouts. You can see that on the VisiBone JavaScript Unit Test page. If you end up doing much JavaScript development, or you practice Extreme Programming principles, you'll find this a great package.
Jeff P. reports that there appears to be some kind of assert() function defined in JavaScript 1.4 (when declaring <script language="JavaScript1.4">).
(c) The JavaScript Card and Foldouts uses assert() for a new and wonderful purpose: it's power to describe.
Look up hammer in the nearest dictionary. Lots of words, maybe a picture. Now imagine the sight of someone using a hammer to drive a nail into wood. Not just a still picture but the motion. Doesn't that describe the essence of hammer quick and thorough? With any tool, showing is so much better than telling. This is what assert() can do: show technology in action.
Example:
assert('abc'.toUpperCase() == 'ABC');If you pretend you can read JavaScript, this little bit of code says a lot. It shows that if 'abc' is the input then 'ABC' is the output. It shows how input and output are handled syntactically. Most of all, since this is real code you can be certain it works as described.
Get used to this assert() function, I know you're gonna love it.
FAQ2: How do you disable right-clicking?
Martin Webb has a great solution here.
FAQ3: Can you help me find a complete online JavaScript reference?
Sure, here are the ones I used:
W3.org's DOM2 standard
ECMA's ECMA-262 standard
Microsoft's JScript (JavaScript)
Microsoft's ECMA compliance
Microsoft's DHTML (DOM)
Netscape's JavaScript Core Index for 1.4
Netscape's JavaScript Central (HTML and PDF for all versions)
Netscape's JavaScript Debugger (very helpful)
Microsoft's Visual Studio has a great JavaScript debugger too
Mozilla's JavaScript References
Errata in the JavaScript Card and Foldouts
Language
s.split() String section, split() does not default to comma delimiting. It just returns s unchanged.asssert → assert in the String section, the first line under // These involve Regular Expressions (thanks, Mattijs)
assertivenenss → assertiveness section heading is misspelled (thanks to Bob Ostermann) I can honestly (though not shamelessly) blame this on a hardware error: the n-key stuttered on an old keyboard.
sieze → seize comment in the Date section (thanks to Charlotte H.)
swutcg ib a → switch on a comment in decision section, foldouts only (hints of carpal tunnel?)
"$" + this.toFixed(2) → "$" + this.cost.toFixed(2) object-orientation section, methods subsection (thanks, Daniel Kabs)
Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY are swapped in the Number section. (thanks, Mark Z.)
DOM
resizeable → resizable in the window.open(u,n,o,h) section for the options parameter text (thanks to Bryan Davis of e-Studio Productions)window.alert() should refer to confirm() and prompt(), not comment(), also in the window.open(u,n,o,h) section (thanks to Steve Erbach)
form.reset() (member function)
form.submit() (member function) warning, does not trigger an onsubmit() event these should be added to the form element section (thanks to David B.)Regular Expressions
\x0000-\xFFFF → \u0000-\uFFFF in the characters inside class [ brackets ] section. (thanks to Igor Z.)Overall
Folodouts → Foldouts on the titles of all three foldouts. Feel the burn.