Skip to content

Instantly share code, notes, and snippets.

@michel-kraemer
Created January 3, 2016 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michel-kraemer/892866038dabcb8376e6 to your computer and use it in GitHub Desktop.
Save michel-kraemer/892866038dabcb8376e6 to your computer and use it in GitHub Desktop.
ECMAScript 6/7 on the JVM with TypeScript and Vert.x
/// <reference path="./vertx-js/vertx.d.ts" />
// arrow functions ********************************************* [1]
vertx.createHttpServer().requestHandler(request => {
// block-scoped variables (let keyword) ********************** [2]
let response = request.response();
response.setChunked(true);
// default parameter ***************************************** [3]
function send(msg = "NOOP") {
response.write(msg);
}
send();
send("\n");
// rest parameters ******************************************* [4]
function sendMulti(...messages) {
messages.forEach(msg => response.write(msg));
}
sendMulti("Hello", " ", "World", "\n");
// spread parameters ***************************************** [5]
let messages = ["Foo", "Bar", "\n"];
sendMulti(...messages);
// for...of loop ********************************************* [6]
let arr = ["1", "+", "1", "=", "2", "\n"];
for (let v of arr) {
send(v);
}
// multi-line template strings, string interpolation ********* [7]
let name = "Elvis";
send(`${name} lives
`);
// classes *************************************************** [8]
class Animal {
eat() {
send("Animal is eating\n");
}
}
class Dog extends Animal {
eat() {
send("Dog is eating\n");
}
walk() {
send("Dog is walking\n");
}
}
class Bird extends Animal {
eat() {
super.eat();
send("Animal is a bird\n");
}
fly() {
send("Bird is flying\n");
}
}
let dog = new Dog();
let bird = new Bird();
dog.eat();
dog.walk();
bird.eat();
bird.fly();
// exponentiation operator *********************************** [9]
let i = 5 ** 2;
send("" + i);
send("\n");
response.end();
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment