33
ECMAScript, 6 Dmitry Soshnikov http://dmitrysoshnikov.com @HelsinkiJS meet-up December 12, 2011

HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

ECMAScript, 6

Dmitry Soshnikov

http://dmitrysoshnikov.com

@HelsinkiJS meet-up December 12, 2011

Page 2: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Function parameter default values

Page 3: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Function parameter default values

function handleRequest(data, method) {

method = method || “GET”;

...

}

Page 4: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Function parameter default values

function handleRequest(data, method) {

method = method || “GET”;

...

}

function handleRequest(data, method = “GET”) {

...

}

Page 5: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Modules system

Page 6: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 7: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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.

Page 8: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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) { ... }

})

Page 9: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Quasi-Literals (String templates)

Page 10: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Quasis : string templates

let content = “<a href=‘” + url + “’ title=‘”

+ title + “’>” + text+ “</a>”;

$(“#bar”).html(content);

Page 11: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 12: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Array comprehensions

Page 13: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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]

Page 14: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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)];

Page 15: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Map and WeakMap

Page 16: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 17: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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!

Page 18: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Destructuring assignment

Page 19: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Destructuring: arrays

// for arrays

let [x, y] = [10, 20, 30];

console.log(x, y); // 10, 20

Page 20: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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”

Page 21: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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);

Page 22: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Eliminating of arguments:

...rest operator

Page 23: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 24: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 25: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

...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

Page 26: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Proxy objects : meta level

Page 27: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 28: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 29: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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

Page 30: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Struct types

Page 31: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

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 } }

]);

Page 32: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Struct types : example

// struct types

let IODataBlock = new StructType( /* attributes */ );

stream.read(IODataBlock, function (block) {

// partial handling

});

Page 33: HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

Thanks for your attention

Dmitry Soshnikov

[email protected]

http://dmitrysoshnikov.com

@DmitrySoshnikov