Slide 1

Slide 1 text

JavaScript for Python Developers Python Meetup 20th August, 2018 Žan Anderle Twitter: @z_anderle

Slide 2

Slide 2 text

Raise your hand if…

Slide 3

Slide 3 text

JavaScript and Python developers

Slide 4

Slide 4 text

https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Overview • JavaScript versions • Basics of the language • JavaScript ecosystem • How to make sense of it all?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Overview • JavaScript history and versions • Basics of the language • Different tools • How to make sense of it all?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Overview • JavaScript history and versions • Basics of the language • Different tools • How to make sense of it all?

Slide 12

Slide 12 text

Syntax 1 let myName = 'Python Meetup'; 2 function sayHi(name) { 3 console.log(`Hey there, ${name}`); 4 } 5 6 sayHi(myName); // 'Hey there, Python Meetup'; 7 8 let someArray = [1, 2, 5, 10]; 9 let newArray = []; 10 11 for (let el of someArray) { 12 if (el > 2) { 13 newArray.push(el); 14 } else { 15 console.log('Nope!'); 16 } 17 } 18 // 'Nope!' 19 // 'Nope!'

Slide 13

Slide 13 text

Syntax 1 class Hero { 2 constructor(name) { 3 this.name = name; 4 } 5 6 superPower() { 7 console.log(`I, ${this.name}, can count really fast!`); 8 let count = 0; 9 while (count < 1000) { 10 count ++; 11 } 12 return count; 13 } 14 } 15 16 let superMan = new Hero('SuperMan'); 17 superMan.superPower(); 18 // I, SuperMan, can count really fast! 19 // 1000 1 class Hero: 2 def __init__(self, name): 3 self.name = name 4 5 def super_power(self): 6 print(f'I, {self.name}, can count really fast!') 7 count = 0 8 while count < 1000: 9 count += 1 10 11 return count 12 13 super_man = Hero('SuperMan') 14 super_man.super_power() 15 # I, SuperMan, can count really fast! 16 # 1000

Slide 14

Slide 14 text

Syntax 1 x = 1 2 # x is a number 3 x = 'Hi!' 4 # x is now a string 5 x = lambda a: a + 1 6 # x is now a function 1 let x = 1; 2 // x is a number 3 x = 'Hi!'; 4 // x is now a string 5 x = a => a + 1; 6 // x is now a function

Slide 15

Slide 15 text

Syntax

Slide 16

Slide 16 text

Syntax

Slide 17

Slide 17 text

Variables 1 var x = 1; 2 let name = 'John'; 3 const someConstant = 45;

Slide 18

Slide 18 text

Variables — scope 1 a = 0 2 3 def func(): 4 b = 10 5 print(a) # 0 6 7 print(b) # Error 8 9 for (i in [1, 2, 3]): 10 print(i) 11 12 print(i) # 3 1 let a = 0; 2 3 function func() { 4 let b = 10; 5 console.log(a); // 0 6 } 7 8 console.log(b); // Error 9 10 for (let i of [1, 2, 3]) { 11 console.log(i); 12 } 13 14 console.log(i); // Error

Slide 19

Slide 19 text

Variables — scope 1 // First js file in your HTML 2 var a = 10; 3 4 // Another js file in your HTML 5 console.log(a); // 10

Slide 20

Slide 20 text

Variable hoisting 1 var x = 1; 2 3 // Some other code 4 5 var name = 'John'; 1 var x; 2 var name; 3 x = 1; 4 5 // Some other code 6 7 name = 'John';

Slide 21

Slide 21 text

Variable hoisting 1 var x = 10; 2 3 function func() { 4 console.log(x); // A lot of code 5 var x = 5; 6 console.log(x); 7 } 8 9 func(); 10 // undefined 11 // 5

Slide 22

Slide 22 text

Variables 1 var factors = [2, 4, 6, 8]; 2 var multipliers = []; 3 4 for (var factor of factors) { 5 multipliers.push(x => x * factor); 6 } 7 8 multipliers[0](2);

Slide 23

Slide 23 text

Variables 1 factors = [2, 4, 6, 8]; 2 multipliers = [] 3 4 for factor in factors: 5 multipliers.append(lambda x: x * factor) 6 7 multipliers[0](2)

Slide 24

Slide 24 text

Variables 1 factors = [2, 4, 6, 8]; 2 multipliers = [] 3 4 for factor in factors: 5 multipliers.append(lambda x: x * factor) 6 7 multipliers[0](2) # 16

Slide 25

Slide 25 text

Variables 1 var factors = [2, 4, 6, 8]; 2 var multipliers = []; 3 4 for (var factor of factors) { 5 multipliers.push(x => x * factor); 6 } 7 8 multipliers[0](2); // 16

Slide 26

Slide 26 text

Variables 1 var factors = [2, 4, 6, 8]; 2 var multipliers = []; 3 4 for (let factor of factors) { 5 multipliers.push(x => x * factor); 6 } 7 8 multipliers[0](2); // 4

Slide 27

Slide 27 text

Data Types • Boolean • String • Number • Null • Undefined • Object let a = true; let b = false; let name = 'John'; name.length; // 4 let num = -124.56; num = 10; let empty = null; let unknown = undefined; let something = {key: 'A value', anotherKey: name}; let things = ['string', 2, (x, y) => { return x + y; }];

Slide 28

Slide 28 text

Object literal 1 let bigObj = { 2 key: 'Some string', 3 add: function(x, y) { return x + y; }, 4 anotherObj: { 5 name: 'I am a nested object' 6 } 7 };

Slide 29

Slide 29 text

Objects are mutable This also includes arrays and functions!

Slide 30

Slide 30 text

Operators 1 if (!a && b) { 2 // Some code 3 } else if (a || b) { 4 // Some code 5 } 1 if not a and b: 2 # Some code 3 elif a or b: 4 # Some code

Slide 31

Slide 31 text

Operators == and != OR === and !==

Slide 32

Slide 32 text

Operators

Slide 33

Slide 33 text

Functions 1 let func = function(a, b) { 2 return a + b; 3 }; 4 5 let func = (a, b) => { return a + b; }; 6 let func = (a, b) => a + b;

Slide 34

Slide 34 text

Functions 1 function add(a, b) { 2 return a + b; 3 } 4 5 function countAndReport(listsToCount, value) { 6 function count(list, value) { 7 let num = 0; 8 for (let el of list) { 9 if (el === value) num++; 10 } 11 return num; 12 } 13 for (let list of listsToCount) { 14 let num = count(list, value); 15 console.log(`I counted ${num} values in list.`); 16 } 17 }

Slide 35

Slide 35 text

Functions 1 class Hero { 2 constructor(name) { 3 this.name = name; 4 } 5 6 superPower() { 7 console.log(`I, ${this.name}, can count really fast!`); 8 } 9 }

Slide 36

Slide 36 text

Functions 1 let pets = { 2 names: ['Simba', 'Nala', 'Scar'], 3 owner: 'Walt', 4 description: function() { 5 return this.names.map( 6 function(pet) { 7 return `${this.owner} knows an awesome cat named ${pet}`; 8 } 9 ); 10 } 11 }

Slide 37

Slide 37 text

Functions 1 function func(a = 1, b = 2) { 2 return a + b; 3 } 4 5 func(5); // 7

Slide 38

Slide 38 text

Functions 1 function func(a = 1, b = 2) { 2 a + b; // no return 3 } 4 5 func(5); // undefined

Slide 39

Slide 39 text

this 1 let pets = { 2 names: ['Simba', 'Nala', 'Scar'], 3 owner: 'Walt', 4 description: function() { 5 return this.names.map( 6 function(pet) { 7 return `${this.owner} knows an awesome cat named ${pet}`; 8 } 9 ); 10 } 11 }

Slide 40

Slide 40 text

this 1 let pets = { 2 names: ['Simba', 'Nala', 'Scar'], 3 owner: 'Walt', 4 description: function() { 5 return this.names.map( 6 (pet) => { 7 return `${this.owner} knows an awesome cat named ${pet}`; 8 } 9 ); 10 } 11 }

Slide 41

Slide 41 text

Classes 1 class Animal(object): 2 def __init__(self, name): 3 self.name = name 4 5 def say_hi(self): 6 print(f'Hi, {self.name}!') 7 8 class Dog(Animal): 9 pass 10 11 dog = Dog('Billy') 12 dog.say_hi() # Hi, Billy! 1 class Animal { 2 constructor(name) { 3 this.name = name; 4 } 5 6 sayHi() { 7 console.log(`Hi, ${this.name}`); 8 } 9 } 10 11 class Dog extends Animal { } 12 13 dog = new Dog('Billy'); 14 dog.sayHi(); // Hi, Billy!

Slide 42

Slide 42 text

Modules 1 from animals import Dog 2 3 dog = Dog('Billy') 4 5 # OR 6 import animals 7 8 dog = animals.Dog('Billy') 1 // animals.js 2 3 export class Dog extends Animal { } 4 5 // main.js 6 7 import { Dog } from 'animals'; 8 dog = new Dog('Billy'); 9 // OR 10 import * as animals from 'animals'; 11 dog = new animals.Dog('Billy');

Slide 43

Slide 43 text

Template literals 1 let a = 5; 2 let b = 10; 3 console.log(`Fifteen is ${a + b} and 4 not ${2 * a + b}.`); 5 // "Fifteen is 15 and 6 // not 20."

Slide 44

Slide 44 text

Template literals 1 let a = 5; 2 let b = 10; 3 console.log('Fifteen is ' + (a + b) + ‘ and\nnot ' + (2 * a + b) + '.'); 4 // "Fifteen is 15 and 5 // not 20."

Slide 45

Slide 45 text

Promises 1 // getPage(url) returns a Promise 2 let loadPageElement = getPage(someApiUrl).then((result) => { 3 // Do something with the result (parse, process) 4 return doSomething(result); 5 }).catch((error) => { 6 handleError(error); 7 }); 8 9 // loadPageElement is also a Promise 10 loadPageElement.then(() => { 11 doSomethingWhenFinished(); 12 });

Slide 46

Slide 46 text

Overview • JavaScript history and versions • Basics of the language • Different tools • How to make sense of it all?

Slide 47

Slide 47 text

Bad Parts • Global variables • == • + • scope

Slide 48

Slide 48 text

TypeScript

Slide 49

Slide 49 text

Overview • JavaScript history and versions • Basics of the language • Different tools • How to make sense of it all?

Slide 50

Slide 50 text

Different tools • npm, bower, yarn • Babel • Webpack • gulp, grunt

Slide 51

Slide 51 text

Different tools • npm, bower, yarn • Babel • Webpack • gulp, grunt

Slide 52

Slide 52 text

Different tools • npm, bower, yarn • Babel • Webpack • gulp, grunt

Slide 53

Slide 53 text

Different tools • npm, bower, yarn • Babel • Webpack • gulp, grunt

Slide 54

Slide 54 text

Different tools • npm, bower, yarn • Babel • Webpack • gulp, grunt

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Overview • JavaScript history and versions • Basics of the language • Different tools • How to make sense of it all?

Slide 57

Slide 57 text

How to get started • Start somewhere • Prepare your codebase • No need to learn and use everything at once

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Thank you! Questions?