CoffeeScript vs. ECMAScript 6

8c367856dfeee6972848cc82c79c0638?s=47 Florian Plank
February 26, 2015

CoffeeScript vs. ECMAScript 6

8c367856dfeee6972848cc82c79c0638?s=128

Florian Plank

February 26, 2015
Tweet

Transcript

  1. 3.
  2. 17.

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

    var age, name; name = "John"; age = 23; ! }).call(this);
  3. 29.

    “ ECMAScript was always an unwanted trade name that sounds

    like a skin disease. ! — Brendan Eich
  4. 30.

    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
  5. 31.
  6. 35.
  7. 36.
  8. 37.
  9. 38.
  10. 39.
  11. 40.
  12. 47.
  13. 49.
  14. 51.

    class Person extends Actor { ! constructor(firstName, lastName) { this.firstName

    = fistName; this.lastName = lastName; } fullname() { return `${this.firstName} ${this.lastName}`; } ! }
  15. 52.

    class Person extends Actor { get name() { // return

    ... } set name(value) { // ... } }
  16. 54.

    Account = (customer, cart) -> @customer = customer @cart =

    cart ! $('.cart').bind 'click', (event) => @customer.purchase @cart
  17. 55.

    function Account(customer, cart) { this.customer = customer; this.cart = cart;

    $('.cart').bind('click', (e) => { this.customer.purchase(this.cart); }); }
  18. 60.

    [name, age] = ["John", 23] ! response = name: "John"

    email: "john@foo.com" age: 23 ! {name, age} = response
  19. 61.

    var options = { repeat: true, save: false }; !

    var { repeat, save } = options;
  20. 62.

    var options = { repeat: true, save: false, rules: {

    custom: 10, } }; ! var { repeat, save, rules: { custom }} = options;
  21. 68.

    var es = []; ! for (var i = 0;

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

    const FOO = "bar"; ! console.log(FOO); // => "bar" !

    FOO = "foo"; ! console.log(FOO); // => "bar" ! const FOO = "baz"; ! console.log(FOO); // => "bar"
  23. 71.

    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
  24. 72.
  25. 73.

    // 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));
  26. 74.
  27. 75.

    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)]); });
  28. 79.
  29. 80.

    var firstName = Symbol(); var person = {}; ! person[firstName]

    = "Nicholas"; console.log(person[firstName]);
  30. 81.

    var firstName = Symbol(); var person = {}; ! person[firstName]

    = "Nicholas"; console.log(person[firstName]);
  31. 82.

    const MY_KEY = Symbol(); let obj = {}; ! obj[MY_KEY]

    = 123; console.log(obj[MY_KEY]);
  32. 84.

    const FOO = Symbol(); ! let obj = { [FOO]()

    { return 'bar'; } }; ! console.log(obj[FOO]());
  33. 86.

    let values = [1, 2, 3]; ! for (let i

    of values) { console.log(i); }
  34. 92.

    users = john: 23, marcy: 29 ! ageReport = for

    name, age of users "#{name} is #{age}"
  35. 96.

    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"
  36. 97.

    [lastName, age] = if name == 'John' then ['Doe', 23]

    else if name == 'Marcy' then ['Murcy', 29] else ['Unknown' ] ! console.log lastName, age
  37. 101.

    invite(user) if user? # typeof user !== "undefined" && user

    !== null ! ! name = userName ? 'John'
  38. 102.

    launch() if ignition is on ! volume = 10 if

    band isnt SpinalTap ! letTheWildRumpusBegin() unless answer is no ! letIn() if name in ['John', 'Marcy']
  39. 103.
  40. 105.
  41. 109.