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

JavaScript: The Magic Parts

JavaScript: The Magic Parts

How do I write JavaScript.

Hannes Moser

October 21, 2016
Tweet

More Decks by Hannes Moser

Other Decks in Programming

Transcript

  1. –Douglas Crockford If a feature is sometimes useful and sometimes

    dangerous and if there is a better option then
 always use the better option.
  2. –Hannes Moser I am going to use any feature I

    am able find within the language!
  3. Object Literals const a = 1 const b = 2

    const o = { a, b, c() { return 'I am C3PO' } }
  4. Arrow Functions const a = [1, 2, 3, 4, 5,

    6] const sumOfIncrementByOneAndOddNumbers = a .map(v => v += 1) .filter(v => v % 2 !== 0 ? v : false) .reduce((sum, v) => sum + v, 0) // 15
  5. Template Strings const foo = 'Hello' const bar = 'World'

    console.log(`${foo}, ${bar}!`)
 // Hello, World!
  6. Tagged Template Strings import html from 'html' const lftCol =

    html`<div class="col-sm-6">left</div>` const rgtCol = html`<div class="col-sm-6">right</div>` const dom = html` <div class="container"> <div class="row"> ${lftCol} ${rgtCol} </div> </div> ` document.body.appendChild(dom)
  7. Named Parameters function complex(name, priority, user, title) { … }

    complex(
 „Hannes Moser",
 undefined,
 null,
 „Mag.(FH)„
 ) // I like this more complex({ name: "Hannes Moser",
 title: "Mag.(FH)" })
  8. Class-free Objects: Prototypes function Pokemon(name) { this.name = name //

    handle ... } Pokemon.prototype.collect = function() { … } const pikachu = new Pokemon("Pikachu")
  9. Class-free Objects: ES2015 Classes class Pokemon { constructor(name) { this.name

    = name // handle ... } collect() { … } } const pikachu = new Pokemon("Pikachu")
  10. Class-free Objects: Constructor Function function pokemon({name} = {}) { function

    collect() { … } return { collect } } const pikachu = pokemon({name: 'Pokemon'})
  11. Class-free Objects: Parasitic Inheritance import parasite from 'parasite' function pokemon({name}

    = {}) { const super = parasite({name}) function collect() { … } super.collect = collect return } const pikachu = pokemon({name: ‚Pokemon'}) pikachu.eat() && pikachu.collect()
  12. Clean Interfaces export default function pokemon() { function collect() {

    … } return {collect} } const pikachu = pokemon() pikachu.bad = function() { … } // no problem at all
  13. Clean Interfaces: Freeze export default function pokemon() { function collect()

    { … } return Object.freeze({collect}) } const pikachu = pokemon() pikachu.bad = function() { … } // throws TypeError or fails silently
  14. Malwaretising import $ from 'jquery' // your friendly malvertising service

    const pref = $.post $.post = function(...args) { pref({ url: 'https://example.com' }) return pref.call(null, ...args) } // example $.post({ url: 'https://example.net' })
  15. mandatory parameters function mandatory() { throw new Error("Missing Parameter") }

    function foo(a = mandatory()) { … } foo() // Error: Missing Parameter
 foo(1)
  16. mixins const Validation = Sup => class extends Sup {

    validate(schema) { … } } class Person { … } class Employee extends Validation(Person) { … }
  17. that’s not async const file = open('hannes.json') const result =

    process(file) const html = render(result) html .then(str => console.log(str)) .catch(err => console.error(err))
  18. that’s not async function open(file) { return new Promise((resolve, reject)

    => { fs.readFile(file, (err, data) => { if (err) { return reject(err) } return resolve(data) }) }) }
  19. Sources • ECMAScript® 2016 Language Specification • Douglas Crockford: The

    Better Parts • Axel Rauschmayr: 2ality.com, Exploring ES6 • Rebecca Murphy: Ain't No Party Like a Third-Party JavaScript Party • Snippets: https://github.com/eliias/the-magic-parts