Upload
dmitry-soshnikov
View
6.669
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
ECMAScript, 6
Dmitry Soshnikov
http://dmitrysoshnikov.com
@HelsinkiJS meet-up December 12, 2011
Function parameter default values
Function parameter default values
function handleRequest(data, method) {
method = method || “GET”;
...
}
Function parameter default values
function handleRequest(data, method) {
method = method || “GET”;
...
}
function handleRequest(data, method = “GET”) {
...
}
Modules system
Modules in ES3, ES5 var DBLayer = (function (global) {
/* save original */
var originalDBLayer = global.DBLayer;
function noConflict() {
global.DBLayer = originalDBLayer;
}
/* implementation */
function query() { ... }
/* exports, public API */
return {
noConflict: noConflict,
query: query
};
})(this);
1. Create local scope 2. Restoring function 3. Implementation 4. Public API
Modules in ES3, ES5 var DBLayer = (function (global) {
/* save original */
var originalDBLayer = global.DBLayer;
function noConflict() {
global.DBLayer = originalDBLayer;
}
/* implementation */
function query() { ... }
/* exports, public API */
return {
noConflict: noConflict,
query: query
};
})(this);
1. Create local scope 2. Restoring function 3. Implementation 4. Public API
Too much of “noise”. A “sugar” is needed.
Modules in ES6 module DBLayer {
export function query(s) { ... }
export function connection(...args) { ... }
}
import * from DBLayer; // import all
// import only needed exports
import {query, connection: attachTo} from DBLayer
query(“SELECT * FROM books”).format(“escape | split”);
attachTo(“/books/store”, {
onSuccess: function (response) { ... }
})
Quasi-Literals (String templates)
Quasis : string templates
let content = “<a href=‘” + url + “’ title=‘”
+ title + “’>” + text+ “</a>”;
$(“#bar”).html(content);
Quasis : string templates
let content = “<a href=‘” + url + “’ title=‘”
+ title + “’>” + text+ “</a>”;
$(“#bar”).html(content);
let content = safeHtml `<a href=“${url} “ title=“${title}”>${text}</a>`;
$(“#bar”).html(content);
See: http://wiki.ecmascript.org/doku.php?id=harmony:quasis
Array comprehensions
Array comprehensions
// map + filter way
let scores = [1, 7, 4, 9]
.filter(function (x) { return x > 5 })
.map(function (x) { return x * x }); // [49, 81]
Array comprehensions
// map + filter way
let scores = [1, 7, 4, 9]
.filter(function (x) { return x > 5 })
.map(function (x) { return x * x }); // [49, 81]
// array comprehensions
let scores = [x * x for (x in values([1, 7, 4, 9])) if (x > 5)];
Map and WeakMap
Map and WeakMap let user = {name: “Ann”, x: 10, y: 20};
// Keys in a map are objects
let scoresMap = new Map;
map.set(user, 100);
console.log(scoresMap.get(user)); // 100
Map and WeakMap let user = {name: “Ann”, x: 10, y: 20};
// Keys in a map are objects
let scoresMap = new Map;
map.set(user, 100);
console.log(scoresMap.get(user)); // 100
let markers = new WeakMap;
marker = new Marker(10, 20);
markers.set(marker, “Ann”);
console.log(weakMap.get(marker)); // “Ann”
delete marker; // if marker is GC’ed, it’s removed from WeakMap too!
Destructuring assignment
Destructuring: arrays
// for arrays
let [x, y] = [10, 20, 30];
console.log(x, y); // 10, 20
Destructuring of function parameters
function Panel(config) {
var title = config.title;
var x = config.pos[0];
var y = config.pos[1];
return title + x + y;
}
new Panel({title: “Users”, pos: [10, 15]});
Too “noisy”
Destructuring of function parameters
function Panel({title: title, pos: [x, y]}) {
return title + x + y;
}
let config = {title: “Users”, pos: [10, 15]};
new Panel(config);
Eliminating of arguments:
...rest operator
Object arguments
// ES3, ES5
function format(pattern /*, rest */) {
var rest = [].slice.call(arguments, 1);
var items = rest.filter(function (x) { return x > 1});
return pattern.replace(“%v”, items);
}
format(“scores: %v”, 1, 5, 3); // scores: 5, 3
Complicated arguments
// ES3, ES5
function format(pattern /*, rest */) {
var rest = [].slice.call(arguments, 1); // complicated
var items = rest.filter(function (x) { return x > 1});
return pattern.replace(“%v”, items);
}
format(“scores: %v”, 1, 5, 3); // scores: 5, 3
...rest
// ES6 aka Harmony
function format(pattern, …rest) { // real array
var items = rest.filter(function (x) { return x > 1});
return pattern.replace(“%v”, items);
}
format(“scores: %v”, 1, 5, 3); // scores: 5, 3
Proxy objects : meta level
Proxy-objects
/* target – original object
* handler – meta-handler */
Proxy(target, handler)
See: http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies
Note: old semantics (currently is implemented in Firefox) will not longer be available:
Proxy.create(handler, [proto]), Proxy.createFunction(handler, [call, [consruct]])
See: http://wiki.ecmascript.org/doku.php?id=harmony:proxies
Proxy-objects
// original object
let point = {
x: 10,
y: 20
};
// proxied object
let loggedPoint = Proxy(point, {
get: function (point, name, rcvr) {
console.log(“get: ”, name);
return point[name];
},
set: function (point, name, value, rcvr) {
console.log(“set: ”, name, value);
point[name] = value;
}
});
Trap of getting of properties
Trap of setting the properties
Proxy-objects
// reading trap
loggedPoint.x; // get: x, 10
// writing trap
loggedPoint.x = 20; // set: x, 20
// reflected on the original object
point.x; // 20
// proxied object
let loggedPoint = Proxy(point, {
get: function (point, name, rcvr) {
console.log(“get: ”, name);
return point[name];
},
set: function (point, name, value, rcvr) {
console.log(“set: ”, name, value);
point[name] = value;
}
});
Meta-handler
Struct types
Struct types // struct types
let Point2D = new StructType({ x: uint32, y: uint32 });
let Color = new StructType({ r: uint8, g: uint8, b: uint8 });
let Pixel = new StructType({ point: Point2D, color: Color });
// array types
let Triangle = new ArrayType(Pixel, 3);
// dense-objects, based on struct binary types
let t = new Triangle([
{ point: { x: 0, y: 0 }, color: { r: 255, g: 255, b: 255 } },
{ point: { x: 5, y: 5 }, color: { r: 128, g: 0, b: 0 } },
{ point: { x: 10, y: 0 }, color: { r: 0, g: 0, b: 128 } }
]);
Struct types : example
// struct types
let IODataBlock = new StructType( /* attributes */ );
stream.read(IODataBlock, function (block) {
// partial handling
});
Thanks for your attention
Dmitry Soshnikov
http://dmitrysoshnikov.com
@DmitrySoshnikov