Erik Arvidsson
GitHub: @arv
Twitter: @ErikArvidsson
leads to:
Today
function Point3D(x, y, z) {
Point.call(this, x, y);
this.z = z;
}
Point3D.prototype = Object.create(
Point.prototype);
Point3D.prototype.constructor = Point3D;
Point3D.prototype.equals = function(p2) {
return Point.prototype.equals.call(this, p2)
&& this.z === p2.z;
};
ES6
class Point3D extends Point {
constructor(x, y, z) {
super(x, y);
this.z = z;
}
equals(p2) {
return super.equals(p2) && this.z === p2.z;
}
}
Today
Today - AMD
define(['./Parser', './SourceFile'],
function(Parser, SourceFile) {
...
return exportedValue;
});
Today - CJS
var Parser = require('./Parser');
var SourceFile = require('./SourceFile');
...
module.exports = exportedValue;
ES6
import {Parser} from './Parser.js';
import {SourceFile} from './SourceFile.js';
...
export class C { ... }
export function f() { ... }
Today
class MenuButton {
constructor(button) {
button.onclick = function(e) {
this.showMenu();
// TypeError: undefined is not a function
};
}
showMenu() { ... }
}
Today
class MenuButton {
constructor(button) {
button.onclick = function(e) {
this.showMenu();
}.bind(this);
}
showMenu() { ... }
}
ES6
class MenuButton {
constructor(button) {
button.onclick = e => this.showMenu();
}
showMenu() { ... }
}
bind(this)
this
(as well as arguments
and super
)
Today
function saveFile(content, filename) {
filename = filename || tempName();
fs.writeFileSync(filename, content);
}
ES6
function saveFile(content,
filename = tempName()) {
fs.writeFileSync(filename, content);
}
Today
function max(x) {
var args =
Array.prototype.slice.call(arguments, 1);
return args.reduce(
(a, b) => a > b ? a : b, x);
}
ES6
function max(x, ...args) {
return args.reduce(
(a, b) => a > b ? a : b, x);
}
arguments
"This is all good but when can I use this?"
function assert(b) {
if (!b) {
throw new Error('Assertion failed');
}
}
// Transforms
assert(someExpression)
// To
void 0
Step 1 - Extend ParseTreeTransformer
class StripAssertTransformer
extends ParseTreeTransformer {
...
}
Step 2 - Override transform method
transformCallExpression(tree) {
var operand = tree.operand;
if (operand.type === IDENTIFIER_EXPRESSION &&
operand.getStringValue() === 'assert') {
return createVoid0();
}
return super.transformCallExpression(tree);
}
Step 3 - Add transformer
// FromOptionsTransformer.js
if (!options.debug) {
append(StripAssertTransformer);
}
Thats it!
Today
/**
* @param {string} s
* @param {number} n
* @return {string}
*/
function repeat(s, n) {
return Array(n + 1).join(s);
}
AtScript
function repeat(s: string, n: number): string {
return Array(n + 1).join(s);
}
AtScript
function repeat(s: string, n: number): string {
return Array(n + 1).join(s);
}
AtScript
function repeat(s, n) {
assert.argumentTypes(s, $tr.types.string,
n, $tr.types.number);
return assert.returnType(
(Array(n + 1).join(s)), $tr.types.string);
}
Today
function repeat(s, n) {
return Array(n + 1).join(s);
}
repeat.metadata = [...];
AtScript
@Description('Repeats a string')
function repeat(s, n) {
return Array(n + 1).join(s);
}
AtScript
function repeat(s, n) {
return Array(n + 1).join(s);
}
repeat.annotations = [
new Description('Repeats a string')];