Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CoffeeScript vs. ECMAScript 6

Florian Plank
February 26, 2015

CoffeeScript vs. ECMAScript 6

Florian Plank

February 26, 2015
Tweet

More Decks by Florian Plank

Other Decks in Programming

Transcript

  1. name = "John" age = 23 ! ! (function() {

    var age, name; name = "John"; age = 23; ! }).call(this);
  2. “ ECMAScript was always an unwanted trade name that sounds

    like a skin disease. ! — Brendan Eich
  3. ES 1 ES 2 ES 3 ES 4 ES 5

    ES 5.1 ES 6 ES 7 1997 1998 1999 — Abandoned 2009 2011 2015 — TBA
  4. class Person extends Actor { ! constructor(firstName, lastName) { this.firstName

    = fistName; this.lastName = lastName; } fullname() { return `${this.firstName} ${this.lastName}`; } ! }
  5. class Person extends Actor { get name() { // return

    ... } set name(value) { // ... } }
  6. Account = (customer, cart) -> @customer = customer @cart =

    cart ! $('.cart').bind 'click', (event) => @customer.purchase @cart
  7. function Account(customer, cart) { this.customer = customer; this.cart = cart;

    $('.cart').bind('click', (e) => { this.customer.purchase(this.cart); }); }
  8. var options = { repeat: true, save: false }; !

    var { repeat, save } = options;
  9. var options = { repeat: true, save: false, rules: {

    custom: 10, } }; ! var { repeat, save, rules: { custom }} = options;
  10. var es = []; ! for (var i = 0;

    i < 10; i++) { let c = i; es[i] = function () { console.log("ES" + c); }; } ! es[6](); // => ES6
  11. const FOO = "bar"; ! console.log(FOO); // => "bar" !

    FOO = "foo"; ! console.log(FOO); // => "bar" ! const FOO = "baz"; ! console.log(FOO); // => "bar"
  12. function* idMaker(){ var index = 0; while(true) yield index++; }

    ! var gen = idMaker(); ! console.log(gen.next().value); // 0 console.log(gen.next().value); // 1 console.log(gen.next().value); // 2
  13. // file A: export const sqrt = Math.sqrt; export function

    square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } ! // file B: import { square, diag } from 'lib'; console.log(square(11)); ! // file C: import * as lib from ‘lib'; console.log(lib.square(11));
  14. function timeout(duration = 0) { return new Promise((resolve, reject) =>

    { setTimeout(resolve, duration); }); } ! var p = timeout(1000).then(() => { return timeout(2000); }).then(() => { throw new Error("hmm"); }).catch(err => { return Promise.all([timeout(100), timeout(200)]); });
  15. var firstName = Symbol(); var person = {}; ! person[firstName]

    = "Nicholas"; console.log(person[firstName]);
  16. var firstName = Symbol(); var person = {}; ! person[firstName]

    = "Nicholas"; console.log(person[firstName]);
  17. const MY_KEY = Symbol(); let obj = {}; ! obj[MY_KEY]

    = 123; console.log(obj[MY_KEY]);
  18. const FOO = Symbol(); ! let obj = { [FOO]()

    { return 'bar'; } }; ! console.log(obj[FOO]());
  19. let values = [1, 2, 3]; ! for (let i

    of values) { console.log(i); }
  20. users = john: 23, marcy: 29 ! ageReport = for

    name, age of users "#{name} is #{age}"
  21. grade = (student) -> if student.excellentWork "A+" else if student.okayStuff

    if student.triedHard then "B" else "B-" else "C" eldest = if 24 > 21 then "Liz" else "Ike"
  22. [lastName, age] = if name == 'John' then ['Doe', 23]

    else if name == 'Marcy' then ['Murcy', 29] else ['Unknown' ] ! console.log lastName, age
  23. invite(user) if user? # typeof user !== "undefined" && user

    !== null ! ! name = userName ? 'John'
  24. launch() if ignition is on ! volume = 10 if

    band isnt SpinalTap ! letTheWildRumpusBegin() unless answer is no ! letIn() if name in ['John', 'Marcy']