Ian Truslove / ian.truslove@nsidc.org / @iantruslove
UCAR Software Engineering Assembly, 1 April 2013
Douglas Crockford - JavaScript: The Good Parts
Thin book, lots of ideas. Disagree with it once you have studied it.
Image credit: http://www.webdesignersreviewofbooks.com/images/69.jpg
// Write some 2d array operations
// to process things like this:
var testData = [
[0.1, 0.2, 0.2, 0.2, 0.1],
[0.1, 0.2, 0.3, 0.2, 0.1],
[0.1, 0.3, 0.4, 0.3, 0.1],
[0.1, 0.2, 0.3, 0.2, 0.1],
[0.1, 0.2, 0.2, 0.2, 0.1] ];
var testData = [
[0.1, 0.2, 0.2, 0.2, 0.1],
[0.1, 0.2, 0.3, 0.2, 0.1],
[0.1, 0.3, 0.4, 0.3, 0.1],
[0.1, 0.2, 0.3, 0.2, 0.1],
[0.1, 0.2, 0.2, 0.2, 0.1] ];
// What's the max value in the array?
ops2d.max(testData);
> 0.4
var ops2d = {
max: function (data) {
var max = ... // calculate max
return max;
}
}
var ops2d = {
max: function (data) {
// using ES5 Array.map
var maxOfEachRow = data.map(maxInRow);
return maxInRow(maxOfEachRow);
}
}
var ops1d = {
max: function (row) { // i.e. maxInRow
// Using ES5 Array.reduce
return row.reduce(
function (acc, current) {
return Math.max(acc, current);
});
}
};
var ops2d = {
max: function (data) {
// Local var was flattened
return ops1d.max(data.map(ops1d.max));
}
};
// Try it...
var testData = [
[0.1, 0.2, 0.2, 0.2, 0.1],
[0.1, 0.2, 0.3, 0.2, 0.1],
[0.1, 0.3, 0.4, 0.3, 0.1],
[0.1, 0.2, 0.3, 0.2, 0.1],
[0.1, 0.2, 0.2, 0.2, 0.1] ];
ops1d.max(testData[0]);
> 0.2
ops2d.max(testData);
> 0.4
// How about scaling the whole data array?
ops2d.scale(testData, 2);
> [ [0.2, 0.4, 0.4, 0.4, 0.2], /* etc ... */ ]
var ops2d = {
scale: function (data, scale) {
return data.map(
function (row) {
return row.map(
function (datum) {
return datum * scale;
});
});
}
};
var ops1d = {
scale: function (row, scale) {
return row.map(
function (datum) { return datum*scale; }
);
}
};
var ops2d = {
scale: function (data, scale) {
return data.map(
function (row) { return ops1d.scale(row, scale); }
);
}
};
ops2d.scale(testData, 2);
> [ [0.2, 0.4, 0.4, 0.4, 0.2], /* ... */ ]
// Note assignment to the object by key:
ops2d.map = function (data, fn) {
return data.map(
function (row) { return row.map(fn); }
);
};
ops2d.map(testData, function (datum) { return datum*2 });
> [ [0.2, 0.4, 0.4, 0.4, 0.2], /* ... */ ]
ops2d.map = function (data, fn) {
return data.map(
function (row) { return row.map(fn); }
);
};
var scaler = function(scale, datum) {
return scale*datum;
};
// Wouldn't it be nice if...
ops2d.map(testData, scaler.curry(2));
// `prototype` is the mechanism for inheritance
Function.prototype.curry = function (firstArg) {
var fn = this;
return (function(x) {
return fn(firstArg, x);
});
};
(scaler.curry(2))(3);
> 6
// A new transformation:
var threshold = function(min, datum) {
return Math.max(min, datum);
};
ops2d.map(testData, threshold.curry(0.2));
> [ [0.2, 0.2, 0.2, 0.2, 0.2],
[0.2, 0.2, 0.3, 0.2, 0.2],
[0.2, 0.3, 0.4, 0.3, 0.2], /* etc ... */
Dec 1999
The JavaScript everyone knows
n/a - abandoned
Contained classes, a module system, optional type annotations and static typing (probably using a structural type system), generators and iterators, destructuring assignment, and algebraic data types
Dec 2009
Incremental improvements over ES3
Strict mode, object getters and setters, seal
and freeze
methods,
new array methods incuding indexOf
, map
, reduce
, filter
tbd
Scaled-down version of ES4
let
, const
, and function
definitions in block scopelet {x, y} = point;
rest
, spread
- ellipsis args in function param listslet identity = (x) => x
[1, 2, 3].map(x => x*x);
Source: http://kangax.github.com/es5-compat-table/, http://kangax.github.com/es5-compat-table/es6/
Stoyan Stefanov - JavaScript Patterns
Covers the basics, through to patterns that are ubiquitous in JS library code.
Image credit: http://www.i-newswire.com/Uploads/64855_2_2.jpgSteve Souders - High Performance Web Sites
Covers web app performance holistically. Essential for any front end developer.
Image credit: http://blog.monitor.us/wp-content/uploads/2012/08/high-performance-web-sites.jpgNick Zakas - High Performance JavaScript
Lots of JavaScript performance knowledge, tips, and tricks.
Image credit: http://d.gr-assets.com/books/1328834985l/7008656.jpgNode.js - native C++ implementation, based on Google V8 engine. Includes npm
package manager.
Rhino - Java implementation of a JS engine, from Mozilla. Used interactively, or embedded to add JS scripting into Java apps.
Envjs - JS implementation of a browser's JS functionality. Uses Rhino, support exists for other back ends. Primarily used for headless testing.
PhantomJS - native headless webkit browser with JS API and REPL. Used for testing, scriptable browser.
JSC - JS parts of WebKit. REPL or command line runtime
npm
)
and ecosystem
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');
source: http://nodejs.org
any application that can be written in JavaScript, will eventually be written in JavaScript
"Atwood's Law", http://www.codinghorror.com/blog/2007/07/the-principle-of-least-power.html
This presentation: http://iantruslove.github.com/serious-javascript-presentation