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

JavaScript for Swift Developers

JavaScript for Swift Developers

A light-hearted talk about misconceptions and struggles of a Swift developer learning about the latest JavaScript features
and oddities.

Marius Rackwitz

May 13, 2017
Tweet

More Decks by Marius Rackwitz

Other Decks in Programming

Transcript

  1. ▸ Both have variable declarations with let. ▸ Swift has

    parameter names. ▸ Different calls to make console prints. ▸ JavaScript has a new operator. ▸ JavaScript has semicolons.
  2. var ▸ In Swift: declares a mutable variable ▸ In

    JavaScript: declares a variable which is hoisted within the function or global scope
  3. let ▸ In Swift: declares an immutable variable, enforced beyond

    re-assignments for value types ▸ In JavaScript: declares a variable which is block-scoped
  4. ;

  5. JAVASCRIPT ▸ 1996: 1.0 ▸ 2000: 1.5 - ECMA 3rd

    edition ▸ 2010: ECMA 5th edition ▸ 2015: ECMA 6th edition - ES6 / ES2015
  6. CLASSES IN SWIFT class Animal : CustomStringConvertible { var age:

    Int init(age: Int) { self.age = age } toString() { let className = String(describing: type(of: self)) return "\(className)(age: \(age))" } } class Dog : Animal { var furColor: String init(age: Int, furColor: String) { super(age: age) self.furColor = furColor; } }
  7. JAVASCRIPT'S PROTOTYPAL INHERITANCE function Animal(age) { this._age = age; }

    Object.defineProperty(Animal.prototype, "age", { get: function() { return this._age; }, }); Animal.prototype.toString = function() { return this.constructor.name + "(age: " + this.age + ")"; } function Dog(age, furColor) { Object.getPrototypeOf(Dog).call(this, age); this.furColor = furColor; } Dog.prototype = Object.create(Animal.prototype, { constructor: { value: Dog } });
  8. ES6 SYNTAX class Animal { constructor(age) { this._age = age;

    } get age() { return this._age; } toString() { return `${this.constructor.name}(age: ${this.age})`; } } class Dog extends Animal { constructor(age, furColor) { super(age); this.furColor = furColor; } }
  9. TYPESCRIPT class Animal { age: number; constructor(age: number) { this.age

    = age; } toString() { return `${this.constructor.name}(age: ${this.age})`; } } class Dog extends Animal { constructor(age: number, furColor: string) { super(age); this.furColor = furColor; } }
  10. ==

  11. ===

  12. // In TypeScript // when compiled with --strictNullChecks function greet(name:

    string) { … } function greet(name: string | undefined = undefined) { … }
  13. SWIFT RUNS ON … ▸ Mac and iOS devices ▸

    Linux ▸ (Android) ▸ (Windows)
  14. JAVASCRIPT HAS … ▸ NPM for Node.js ▸ Different approaches

    for frontend code: Bower or Browserify / Webpack etc.
  15. import BananaKit from 'bananakit'; import Monkey from 'monkey'; const monkey

    = new Monkey(); const tree = new BananaKit.Tree(); monkey.visit(tree); // => TypeError: m.climb is not a function // at tree.accept (bananakit.js) // at monkey.visit (monkey.js)