@rcoder :: http://rcoder.net/
- Language history
- Useful patterns
- Hope
- Created as LiveScript by Brendan Eich in 1995
- Renamed + recast in a supporting role for Java
- Used to build roll-overs, animated text, other annoyances
- IE won an effective browser monopoly
- JS stagnated while MS built C# and the CLR
- ...and then, Oddpost + GMail made it important again
- Functions
- Objects
- Core types: Object, Array, RegExp, Date, etc.
- Executable code
- Lexical environment
- First-class objects
- Built at MIT 1975-80, standardized in RnRS specs
- Clean, research-oriented dialect of Lisp
- Functions are first-class values
- Programs operate on themselves
(define (make-greeter msg)
(lambda (s)
(string-append msg s)))
(define yo-greeter (make-greeter "Yo "))
@@@JavaScript
var makeGreeter = function(msg) {
return function(s) {
return msg + s;
}
}
var yoGreeter = makeGreeter("Yo ");
- Weak standard library
- Many (incompatible) implementations
- Considered a "toy language" by many programmers
- Namespace
- Attributes
- Prototype
- Built at PARC, released by Sun in 1990
- Objects have slots
- Slots contain data or methods
- All interaction is via message-passing
- Designed for long-lived objects
- No fixed classes, so objects can evolve
- Any number of parent slots can be searched for missing slots
- Traits and prototypes are special cases of parents
(Image copyright © Oracle Corp.)
- JavaScript objects have at most one prototype
- DOM nodes inherit properties from containers
- "is-a" vs. "is-in"
@@@ JavaScript
function Resource(url) {
this.url = url;
this.ready = false;
this.data = {};
}
Resource.prototype.get = function(func) {
this.ready = false;
$.get(this.url, function(data) {
this.data = data;
this.ready = true;
if (func) { func(this); }
}, 'json');
}
- I hated Javascript for many years
@@@ HTML
<!-- mixing markup and code -->
<input name="first_name" type="text"
onchange="return
checkField(this,'required field')"/>
<script>
// globals, globals, everywhere
globalElem =
document.getElementById('#an-elem');
processGlobalElem();
</script>
- Write JavaScript like its weird parents
evalwithglobals
@@@ JavaScript
function Counter() {
var value = 0;
this.incr = function() {
value += 1;
}
this.getValue() {
return value;
}
return this;
}
@@@ JavaScript
$(".required-field").blur(function() {
var me = $(this);
var value = me.val();
if (!value) {
me.append("<em>* required</em>");
}
});
- Finally, a standard library for JavaScript
- Encourages good habits
- Uses Self-like slots with getters/setters
- Bind simple callback functions, rather than classes
- Still needs data-modeling support
- Also bound to browser concurrency (or lack thereof)
- Concurrency
- New Syntax
- Parallel, sandboxed processes
- Pass messages to and from calling page
- Unable to access the DOM, network, or filesystem
- Function like processes from Erlang (but heavy-weight!)
- No shared state, so you can't deadlock
- Available in Firefox, Safari, Chromium/Chrome, Opera
@@@ JavaScript
var worker = new WebWorker('doubler.js');
worker.onMessage = function (event) {
console.log(event.data.toString());
}
worker.postMessage("2");
@@@ JavaScript
// doubler.js
onMessage = function(event) {
var data = event.data;
if (data) {
var number = Number(data);
postMessage(number * 2);
}
}
-
Binds variables within the current block
-
Protects variables with the same name in outer blocks
-
Like 'letrec' in Scheme, 'my' in Perl
-
Available in JS 1.7 (i.e., only Firefox)
@@@ JavaScript var x = 5;
if (true) { let x = 6; console.log(x); // => prints "6" }
console.log(x); // => prints "5"
- Called 'pattern matching' in Haskell, ML
- Limited form (array/list unpacking) also in P* languages
@@@ JavaScript
// array destructuring
var (a, b, c) = [1, 2, 3];
// object destructuring
var obj = {
name: "an object",
size: [10, 10]
};
var {name, size} = obj;
- Generators, iterators
- 'const' bindings
- Modules
- ...very little of it coming all that soon :(
- JavaScript is actually a cool language
- Sadly, shackled to weak/inconsistent runtime
- Libraries are helping, but the language needs to evolve
- Learning weird research langauges will help your JS coding