Slide 1

Slide 1 text

JAVASCRIPT for Swift Developers

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

SWIFT let dog = Dog(age: 3, furColor: "brown") print(dog) // => "Dog(age: 3)"

Slide 5

Slide 5 text

JAVASCRIPT let dog = new Dog(3, "brown"); console.log(dog); // => "Dog(age: 3)"

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

SUPERFICIAL

Slide 8

Slide 8 text

▸ Both have variable declarations with let. ▸ Swift has parameter names. ▸ Different calls to make console prints. ▸ JavaScript has a new operator. ▸ JavaScript has semicolons.

Slide 9

Slide 9 text

VARIABLE DECLARATIONS ▸ var ▸ let ▸ const

Slide 10

Slide 10 text

var ▸ In Swift: declares a mutable variable ▸ In JavaScript: declares a variable which is hoisted within the function or global scope

Slide 11

Slide 11 text

let ▸ In Swift: declares an immutable variable, enforced beyond re-assignments for value types ▸ In JavaScript: declares a variable which is block-scoped

Slide 12

Slide 12 text

const ▸ Only in JavaScript: declares a variable which is block-scoped and not re-assignable

Slide 13

Slide 13 text

;

Slide 14

Slide 14 text

WRITE MULTIPLE STATEMENTS IN A SINGLE LINE

Slide 15

Slide 15 text

IN SWIFT IT'S OPTIONAL.

Slide 16

Slide 16 text

IN JAVASCRIPT IT'S SOMETIMES OPTIONAL.

Slide 17

Slide 17 text

AUTOMATIC SEMICOLON INSERTION

Slide 18

Slide 18 text

return { name: "This is fine." };

Slide 19

Slide 19 text

return; { name: "This is fine." };

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

semi: ["error", "always"] // or: ["error", "never"]

Slide 23

Slide 23 text

BUT THERE IS MORE THAN THE SUPERFICIAL…

Slide 24

Slide 24 text

STEP BACK What do we compare?

Slide 25

Slide 25 text

SWIFT ▸ 2014: 1.0 ▸ … ▸ 2017-03-27: 3.1

Slide 26

Slide 26 text

JAVASCRIPT ▸ 1996: 1.0 ▸ 2000: 1.5 - ECMA 3rd edition ▸ 2010: ECMA 5th edition ▸ 2015: ECMA 6th edition - ES6 / ES2015

Slide 27

Slide 27 text

ENGINES ▸ JavaScriptCore ▸ V8 ▸ SpiderMonkey, Chakra, Carakan, …

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

SWIFT & JAVASCRIPT SUPPORT DIFFERENT PROGRAMMING PARADIGMS.

Slide 30

Slide 30 text

▸ Imperative Programming ▸ Object-oriented Programming ▸ Declarative Programming ▸ Functional Programming ▸ Many things in between …

Slide 31

Slide 31 text

Let's talk about TYPES

Slide 32

Slide 32 text

SWIFT HAS A STRONG TYPE SYSTEM.

Slide 33

Slide 33 text

JAVASCRIPT HAS A DYNAMIC TYPE SYSTEM.

Slide 34

Slide 34 text

INHERITANCE

Slide 35

Slide 35 text

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; } }

Slide 36

Slide 36 text

JAVASCRIPT HAS MANY ⠀ ⠀

Slide 37

Slide 37 text

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 } });

Slide 38

Slide 38 text

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; } }

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

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; } }

Slide 41

Slide 41 text

WHY THE UGLY? UNDER THE HOOD IT'S STILL THE SAME!*

Slide 42

Slide 42 text

EQUALITY?

Slide 43

Slide 43 text

SWIFT HAS ==.

Slide 44

Slide 44 text

SEMANTICS ARE ENCODED IN THE STANDARD LIBRARY AND EXTENSIBLE.

Slide 45

Slide 45 text

extension Animal : Equatable {} public function ==(lhs: Animal, rhs: Animal) { return lhs.age == rhs.age; }

Slide 46

Slide 46 text

JAVASCRIPT HAS MORE: == & ===

Slide 47

Slide 47 text

==

Slide 48

Slide 48 text

===

Slide 49

Slide 49 text

ROLL YOUR OWN class Animal { … isEqual(other) { return this.age === other.age; } }

Slide 50

Slide 50 text

Let's talk about ⠀⠀⠀⠀⠀⠀⠀⠀⠀ null

Slide 51

Slide 51 text

Swift has an explicit concept of nullability encoded in the type system.

Slide 52

Slide 52 text

JavaScript hasn't. IN ADDITION TO null, EVERYTHING CAN BE undefined.

Slide 53

Slide 53 text

null ▸ typeof null => "object" ▸ Literal

Slide 54

Slide 54 text

undefined ▸ typeof undefined => "undefined" ▸ Property of the global object ▸ Can be overwritten !

Slide 55

Slide 55 text

! TYPESCRIPT TO THE RESCUE!

Slide 56

Slide 56 text

let u: undefined = undefined; let n: null = null;

Slide 57

Slide 57 text

// In Swift func greet(name: string) { … } func greet(name: string?) { … }

Slide 58

Slide 58 text

// In TypeScript // when compiled with --strictNullChecks function greet(name: string) { … } function greet(name: string | undefined = undefined) { … }

Slide 59

Slide 59 text

ECO SYSTEM

Slide 60

Slide 60 text

SWIFT RUNS ON … ▸ Mac and iOS devices ▸ Linux ▸ (Android) ▸ (Windows)

Slide 61

Slide 61 text

JAVASCRIPT RUNS … everywhere

Slide 62

Slide 62 text

JavaScript is an assembly language. — Erik Meijer

Slide 63

Slide 63 text

ASM.JS

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Let's talk about ⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀ ⠀ ⠀

Slide 66

Slide 66 text

The Dream IMPLEMENT THE APP ONCE & DEPLOY IT ON ALL PLATFORMS

Slide 67

Slide 67 text

REALITY? Platforms have different requirements!

Slide 68

Slide 68 text

SHARE THE BUSINESS LOGIC But not UI ⠀ ⠀

Slide 69

Slide 69 text

! DEPENDENCY MANAGEMENT

Slide 70

Slide 70 text

Reminder SWIFT IS INTEROPERABLE WITH OBJECTIVE-C

Slide 71

Slide 71 text

OBJECTIVE-C DEVELOPERS HISTORICALLY USED … ▸ No dependencies ▸ Git Submodules ▸ CocoaPods

Slide 72

Slide 72 text

WITH SWIFT, MOST USE: ▸ CocoaPods ▸ Carthage ▸ Swift Package Manager

Slide 73

Slide 73 text

NO CENTRAL CODE REGISTRY, YOU RELY ON PRIVATE HOSTED REPOSITORIES.

Slide 74

Slide 74 text

JAVASCRIPT HAS … ▸ NPM for Node.js ▸ Different approaches for frontend code: Bower or Browserify / Webpack etc.

Slide 75

Slide 75 text

NPM IS A PACKAGE MANAGER AND A PLATFORM.

Slide 76

Slide 76 text

YOU SUBMIT ACTUAL CODE.

Slide 77

Slide 77 text

! NO FULL DEPENDENCY RESOLUTION BY DEFAULT

Slide 78

Slide 78 text

PITFALLS OF RECURSIVE RESOLUTION [email protected] !"" [email protected] # $"" [email protected] $"" [email protected]

Slide 79

Slide 79 text

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)

Slide 80

Slide 80 text

DIFFERENT APPROACHES FOR LOCKING YOUR DEPENDENCIES ! ▸ Commit node_modules ▸ npm shrinkwrap ▸ Yarn

Slide 81

Slide 81 text

⠀ ⠀ ⠀ ⠀ THE STATE OF AFFAIRS?

Slide 82

Slide 82 text

questions.forEach((question) => { question.ask(); });

Slide 83

Slide 83 text

THANKS FOR YOUR ATTENTION! @MRACKWITZ [email protected]