2. The Big Picture
ECMAScript 3
JavaScript 1.5 ActionScript 2 JScript Etc.
SpiderMonkey AVM JScript Engine KJS (Apple)
Rhino Opera
3. Jav
aS
1999
crip
t1
.5
Jav
2005
aS
crip
t1
.6
Jav
2006
aS
crip
t1
.7
Jav
aS
2008
c
Jav ript
aS 1.8
cr
ipt
Ja
Path to JavaScript 2
1.9
va
2009
Sc
ri
pt
2
5. JavaScript 1.5
Getters and Setters
ā¦
ā¦ Extra power over properties
ā¦ Two powerful uses:
ā¦ Private data access
ā¦ Lazy-loading data
var n = āJohnā;
ā¦
var obj = {
get name(){ return n; },
set name(value){ n = value; }
};
obj.name = āTedā;
obj.name == āTedā // true
6. Getters and Setters
var n = āJohnā;
ā¦
var obj = {};
obj.__deļ¬neGetter__(ānameā,
function(){ return n; });
obj.__deļ¬neSetter__(ānameā,
function(value){ n = value; });
obj.name = āTedā;
obj.name == āTedā; // true
obj.__lookupGetter__(ānameā)
ā¦
// => function(){ return n; }
// __lookupSetter__ too!
7. Lazy-Loading Data
var pixelsDone;
ā¦
data.__deļ¬neGetter__(āpixelsā, function(){
if ( pixelsDone )
return pixelsDone;
pixelsDone = [];
for ( var i = 0; i < pixels.length; i += 4 ){
pixelsDone.push( p.color(...) );
}
return pixelsDone;
});
obj.pixels[3]
ā¦
8. JavaScript 1.6
Array Extras
ā¦
ā¦ indexOf / lastIndexOf
[0,1,2].indexOf(1) == 1
ā¦ every / ļ¬lter / some
[0,1,2].every(function(val){ return val >
0; }); // false
ā¦ map
[0,1,2].map(function(val){return val + 1;});
// [1,2,3]
ā¦ forEach
[0,1,2].forEach(function(val,index){});
9. JavaScript 1.7
Generators and Iterators
ā¦
ā¦ Lazy-load data
ā¦ Database querying
ā¦ Threading
http://www.neilmix.com/2007/02/07/
threading-in-javascript-17/
10. Generators and Iterators
function ļ¬b() {
ā¦
var i = 0, j = 1;
while (true) {
yield i;
var t = i;
i = j; j += t;
}
}
var g = ļ¬b();
for ( var i in g ) {
document.write(i + ā<br>nā);
if ( i > 100 ) break;
}
11. let
Block-level statements
ā¦
for ( let i = 0; i < 10; i++ ) { }
ā¦
while ( true ) { let test = 5; }
ā¦
let (test = 5) {
ā¦
function stuļ¬(){ return test; }
}
{ let test = 5;
ā¦
{ let test = 6; print(test);}
print(test); }
12. Array Comprehension
[i for ( let i = 0; i < 3; i++ )]
ā¦
// => [0, 1, 2]
function range(begin, end) {
ā¦
for (let i = begin; i < end; ++i) {
yield i;
}
}
[i for each (i in range(0, 3))]
13. Destructuring
var [newA, newB] = [a, b];
ā¦
var newA = a, newB = b;
ā¦
[a, b] = [b, a]
ā¦
function test(){ return [0, 1]; }
ā¦
let [a, b] = test();
var [, c] = test();
var {name: name} = {name: āJohnā}
ā¦
name == āJohnā
var {name} = {name: āJohnā}
ā¦
14. JavaScript 1.8
Mostly a bug ļ¬x release
ā¦
Expression Closures
ā¦
function(x){ return x * x; }
function(x) x * x;
Generator Expressions
ā¦
function val_iter(obj) {
return (obj[x] for (x in obj));
}
for ( let value in val_iter(obj) ) { ... }
15. reduce
Great for summing or concating
ā¦
[x for ( x in 100 )]
.reduce(function(a,b) a+b);
[[...],[...]]
ā¦
.reduce(function(a,b){ a.concat(b);}, [])
reduceRight
ā¦
19. Catchalls
var props = {};
ā¦
var obj = {
test: true,
get *(name){
return props[name];
},
set *(name, value){
props[name] = value;
}
}
obj.__deļ¬neGetter_(āā, function(){})
ā¦
20. JavaScript 2 Goals
Backwards Compatible
ā¦
Suitable to developing large systems
ā¦
Allow for reusable libraries
ā¦
Merge previous eļ¬orts (ActionScript)
ā¦
Fix ECMAScript 3 bugs
ā¦
Keep it usable for small programs
ā¦
21. Features
Classes and Interfaces
ā¦
Packages and Namespaces
ā¦
Type Annotations
ā¦
Strict Veriļ¬cation
ā¦
Optimization
ā¦
Syntax Shortcuts
ā¦
Iterators and Generators
ā¦
Self-hosting
ā¦
22. The Direction
ECMAScript 4
JavaScript 2 ActionScript 4 JScript Etc.
Screaming
Tamarin KJS (Apple)
Monkey
Opera
23. Tamarin
Tamarin
ā¦
ā¦ New Virtual Machine from Adobe
ā¦ Perfect for ActionScript
ā¦ (a mutant cousin of JavaScript 2)
The Three Monkeys:
ā¦
ā¦ ActionMonkey
ā¦ ScreamingMonkey
ā¦ IronMonkey
24. Three Monkies
ActionMonkey
ā¦
ā¦ Integrating Tamarin into SpiderMonkey
ā¦ Powering Firefox 4 (?) + JavaScript 2
ScreamingMonkey
ā¦
ā¦ Forcing Tamarin into Internet Explorer
ā¦ (Kicking and screaming?)
IronMonkey
ā¦
ā¦ Bringing Python + Ruby to Tamarin
26. Classes
class Programmer {
ā¦
var name;
var city = āBoston, MAā;
const interest = ācomputersā;
function work() {}
}
var p = new Programmer;
ā¦
p.name = āJohnā;
p.work();
p.work.apply( someotherp );
p.interest = āscienceā; // Error
27. Dynamic Classes
dynamic class Programmer {
ā¦
var name;
var city = āBoston, MAā;
const interest = ācomputersā;
function work() {}
}
var p = new Programmer;
ā¦
p.lastName = āResigā;
for ( var i in p )
alert( i );
// alert( āResigā );
28. Getters and Setters
class Programmer {
ā¦
var _name;
function get name(){ return _name; }
function set name(value){
_name = value + ā Resigā;
}
}
var p = new Programmer;
ā¦
p.name = āJohnā;
alert( p.name );
// āJohn Resigā
29. Catch-Alls
dynamic class Programmer {
ā¦
meta function get(name) { ... }
meta function set(name, value) {
alert(āSetting ā + name + ā to ā + value);
}
}
var p = new Programmer
ā¦
p.name = āJohnā;
// alert(āSetting name to Johnā);
30. Inheritance
class Artist {
ā¦
function draw() { alert(āDrawing!ā); }
}
class Designer extends Artist {
override function draw() {
alert(āDesigning!ā);
}
}
var d = new Designer
ā¦
d.draw();
// alert(āDesigning!ā);
31. Inheritance (cont.)
āļ¬nalā methods canāt be overriden
ā¦
class Artist {
ā¦
ļ¬nal function draw() {alert(āDrawing!ā);}
}
class Designer extends Artist {
// ERROR: Canāt override draw!
override function draw() {
alert(āDesigning!ā);
}
}
32. Inheritance (cont.)
āļ¬nalā classes canāt be inherited from
ā¦
ļ¬nal class Artist {
ā¦
function draw() { alert(āDrawing!ā); }
}
// ERROR: Canāt inherit from Artist
class Designer extends Artist {
override function draw() {
alert(āDesigning!ā);
}
}
33. Metaclass
Provide global functions and properties on
ā¦
a class object
class Users {
ā¦
static function ļ¬nd( name ) {
// ...
}
}
Users.ļ¬nd( āJohnā );
ā¦
34. Interfaces
Verify that a class implements another
ā¦
class Artist {
ā¦
function draw();
}
class Designer implements Artist {
function draw() { alert(āDesigning!ā); }
}
if ( Designer is Artist )
ā¦
alert(āDesigners are Artists!ā);
36. Types
Types are broken down into:
ā¦
ā¦ string
ā¦ double (ECMAScript 3-style Number)
ā¦ boolean
37. Type Annotations
var name : string = āJohnā;
ā¦
let x : double = 5.3;
ā¦
function stuļ¬( x: string, obj: Object ) :
ā¦
boolean {}
38. Function Types
Only return speciļ¬c types:
ā¦
function isValid() : boolean { }
Only be used on certain objects types:
ā¦
function every( this: Array ) {
for ( var i = 0; i < this.length; i++ )
alert( this[i] );
}
every.call( [0,1,2] );
// alert(0); alert(1); alert(2);
every.call({a: ābā});
// ERROR
40. Union and Any Types
var test : (string, double) = ātestā;
ā¦
test = 3;
test = {}; // ERROR
These are equivalent:
ā¦
ā¦ var test : * = ātestā;
ā¦ var test = ātestā;
41. Type Deļ¬nitions
type Point = { x: double, y: double };
ā¦
var p : Point = { x: 3.0, y: 24.0 };
ā¦
42. Nullability
Prevent variables from accepting null
ā¦
values
var name : * = āJohnā;
ā¦
var name : string! = āJohnā;
ā¦
name = āTedā;
name = null; // ERROR
function test( name: string? ) {
ā¦
alert( name );
}
43. Initialization
class User {
ā¦
var name : string!; // Must be initialized
function User( n ) : name = n {
// ...
}
}
44. ālikeā
type Point = { x: double, y: double };
ā¦
if ( { x: 3, y: 5 } like Point )
ā¦
alert( āThat looks like a point to me!ā );
if ( !({ x: 3 } like Point) )
ā¦
alert( āNot a point!ā );
// Loop over array-like things:
ā¦
function every( a: like { length: double } ) {
for ( var i = 0; i < a.length; i++ )
alert( a[i] );
}
45. Parameterized Types
var m: Map.<string, *>;
ā¦
class Point.<T> {
ā¦
var x: T, y: T;
}
var p = new Point.<double>;
ā¦
p.x = 3.0;
p.y = 5.0;
48. Multimethods
generic function intersect(s1, s2);
ā¦
generic function intersect(s1: Shape, s2: Shape ) {
// ...
}
generic function intersect(s1: Rect, s2: Rect ) {
// ...
}
generic function test(a: Object?, b...
ā¦
generic function test(a: Object!
ā¦
49. Iteration
class Fib {
ā¦
private var a=0, b=1;
iterator function get(deep:boolean = false) : IteratorType.<double>
this
public function next(): double {
if ( b > 100 )
throw iterator::StopIteration;
[a,b] = [b,a+b];
return a;
}
}
for ( let i in new Fib )
ā¦
alert( i );
50. For .. Each
For each loops through values
ā¦
let s = āā;
ā¦
for each ( let n in [āaā,ābā,ācā] )
s += n;
alert(s);
// āabcā
52. Map.es
The reference implementationās classes are
ā¦
written in ECMAScript
package
ā¦
{
use namespace intrinsic;
use default namespace public;
intrinsic class Map.<K,V>
{
static const length = 2;
function Map(equals=intrinsic::===, hashcode=intrinsic::hashcode)
: equals = equals
, hashcode = hashcode
, element_count = 0
{
}
// ...
}
53. More Info
ECMAScript 4 Progress:
ā¦
http://spreadsheets.google.com/ccc?key=pFIHldY_CkszsFxMkQOReAQ&hl=en
ECMAScript 4 White Paper Overview:
ā¦
http://www.ecmascript.org/es4/spec/overview.pdf
Blogging:
ā¦
http://ejohn.org/