Modules in ES3, ES5
var DBLayer = (function (global) {
/* save original */
var originalDBLayer = global.DBLayer;
function noConflict() {
global.DBLayer = originalDBLayer; 1. Create local scope
} 2. Restoring function
/* implementation */ 3. Implementation
4. Public API
function query() { ... }
/* exports, public API */
return {
noConflict: noConflict,
query: query
};
})(this);
Modules in ES3, ES5
var DBLayer = (function (global) {
/* save original */
var originalDBLayer = global.DBLayer;
function noConflict() {
global.DBLayer = originalDBLayer; 1. Create local scope
} 2. Restoring function
/* implementation */ 3. Implementation
4. Public API
function query() { ... }
/* exports, public API */
Too much of “noise”.
return { A “sugar” is needed.
noConflict: noConflict,
query: query
};
})(this);
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) { ... }
})
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 of
function parameters
function Panel(config) {
var title = config.title;
var x = config.pos[0]; Too “noisy”
var y = config.pos[1];
return title + x + y;
}
new Panel({title: “Users”, pos: [10, 15]});
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);
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 // proxied object
let point = { let loggedPoint = Proxy(point, {
x: 10, get: function (point, name, rcvr) {
y: 20 console.log(“get: ”, name);
}; return point[name];
},
set: function (point, name, value, rcvr) {
Trap of getting of console.log(“set: ”, name, value);
properties
point[name] = value;
}
Trap of setting the });
properties