TSSJS Day 3: Ted Neward’s ECMAScript Talk

Ted gets back to the basics with JavaScript (JS)…I mean ECMAScript (ES)!

Numbers are all 64-bit floating point…no ints!  Strings are 16-bit Unicode. JS doesn’t use typed references…”var” is just for the parser to know a variable is about to follow.  If you don’t use var, it’s global (anti-pattern). Coercion is always possible…the var is typeless but the object it points to are typed, just not very strongly. ES is a classless system.

In Java6, they introduced “jrunscript” uses the JSR-223 to figure out by extension what script you’re trying to run (e.g., JS, Groovy, Python, etc.).

Boolean expressions are false if false, null, undefined, “”, 0 and NaN.  Truth is everything else.  In JS, you always return something (undefined if nothing else). == always attempts coercion, but the triple equals (===, !==) behaves how Java devs think it should work.

JS has 3 types that you should care about, Object, function and undefined. , [] () are “refinement” operators.  Any use of () evaluates into function object (aka functor).  You can do this:

var alertType = typedef(alert)
if (alertType === 'undefined')
    alert = println

For functions, “this” is a reference which its contents vary with invocation pattern. Defined parameters are completely unnecessary.  You can use “arguments” to get the arguments passed in from the calling function.

JS is bound to function scope not block scope.  This means you should do var declarations at the top of the function not the top of the block, like in C/Java.

Objects are really just HashMaps or key/value bags.

Beyond this point Ted’s talk blew my mind.  He brought up some pretty interesting examples with closures, currying and Fibonacci example.  It’s probably better to just check out his slides here as I won’t do it justice.