Slide 1

Slide 1 text

JavaScript Bootcamp 2019 2019, 12th April @ Recruit Technologies Kento TSUJI @maxmellon

Slide 2

Slide 2 text

ࣗݾ঺հ • Github: @maxmellon • Twitter: @maxmellon_9039 • ϦΫϧʔτςΫϊϩδʔζ 18೥ ৽ଔೖࣾ • ࠷ۙ Apex ΍ͬͯΔΑʙ

Slide 3

Slide 3 text

΍Δ͜ͱɺ΍Βͳ͍͜ͱ ΍Δ͜ͱ ΍Βͳ͍͜ͱ ɾ ɾ جຊతͳه๏ ɾ ΫϥΠΞϯτ࣮૷ ɾ αʔόʔ࣮૷ ɾ ςετίʔυ࣮૷ ɾ σϓϩΠ ޙͰݟ͓ͯ͘ͱྑͦ͞͏ͳ΋ͷ https://github.com/asciidwango/js-primer js-primer Ͱݕࡧ

Slide 4

Slide 4 text

ϑϩϯτΤϯυΤϯδχΞͱ JavaScript • ϦΫϧʔτςΫϊϩδʔζͷ 
 “ϑϩϯτΤϯυΤϯδχΞ” ͷఆٛ͸޿ͯ͘ਂ͍ • HTML/CSS/JSΛ۶ࢦͯ͠ΫϥΠΞϯτΛ
 ࡞Δ͚ͩͰ͸ͳ͍ • ϑϩϯτΤϯυͷͨΊͷαʔόʔ΍ύϑΥʔϚϯ ενϡʔχϯάɺϢʔβϏϦςΟ޲্ͳͲ༷ʑ

Slide 5

Slide 5 text

ϦΫϧʔτͷ
 ϑϩϯτΤϯδχΞͬͯ ͳʹ͢Δͷʁ

Slide 6

Slide 6 text

ࣗ෼ͷ৔߹

Slide 7

Slide 7 text

ۀ຿಺༰ • Airγϑτͱ͍͏αʔϏεͷ
 ϑϩϯτΤϯυΛ։ൃ͍ͯ͠Δ • ࠷ۙ͸ओʹύϑΥʔϚϯενϡʔϯϯά • ώΞϦϯάߦͬͨΓ΋͢Δ͠
 Ϣʔβʔମݧ࠷େԽͷͨΊʹͳΜͰ΋΍Δ

Slide 8

Slide 8 text

Airγϑτ ͱ͸

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

"JSγϑτར༻͍ͯ͠ΔͷͰ͕͢ Ξοϓσʔτ͕͔͋ͬͯΒ ͘͢͝ॏ͍ΜͰ͢ΑͶʜ Ͳ͏ʹ͔ͯ͠
 ૣ͘ͳΒͳ͍Ͱ͠ΐ͏͔ʁ ͳΔ΄Ͳʜ ࣗ෼ͷύιίϯ͕ѱ͍ͷͰ͠ΐ͏͔ʁ ΫϥΠΞϯτ ΅͘ ↓ ͋Δ೔

Slide 13

Slide 13 text

࣮ࡍʹݟʹߦ͘ͷ΋࢓ࣄ

Slide 14

Slide 14 text

- ஗͍ݪҼ͸αʔόʔͰ͸ͳ͔ͬͨ - ΫϥΠΞϯτͷPC͕௿εϖοΫͩͬͨ - ։ൃऀʹൺ΂ͯ௿εϖοΫͳPC͕ଟ͘ར༻͞Ε͍ͯΔ ݹ͍ ஗͍ ฉ͍ͯΈΔͱ

Slide 15

Slide 15 text

• γϑτͷྔ͕༧૝ΑΓଟ͔ͬͨ • άϧʔϓػೳͰళฮͷछผΛ͍ͯͨ͠ • HTMLͷඳըΑΓJavaScriptͷ࣮ߦ͕஗͔ͬͨ ϘτϧωοΫͷ΄ͱΜͲ͕ +BWB4DSJQUͷ࣮ߦ ˋ 9.8ඵ 1.1ඵ 3.5ඵ 0.1ඵ JavaScriptͷ࣮ߦ HTMLͷඳը CSSͷద༻ ͦͷଞ ࣮ࡍʹ๚໰͢Δͱ

Slide 16

Slide 16 text

ώΞϦϯάͱσʔλ͔Β ௐࠪͯ͠ϘτϧωοΫΛಛఆ

Slide 17

Slide 17 text

΋ͨ͘͞Μͷ໰୊Λ ಛఆɺղܾ ϝϯόʔζϒϩάʹ ܝࡌࡁΈ

Slide 18

Slide 18 text

13.5 ඵ͔Β 2.6ඵʹվળ After Before 2.6ඵ 13.5ඵ ౦ژNodeֶԂࡇ ‘18 HTML5 Conferenceʹͯ ঺հ 11ඵ ແବΛΧοτ ඳը׬ྃ ඳը׬ྃ ܭࢉ΍Ϧιʔεͷऔಘ ݁Ռ 50%ͷϢʔβʔͰ 5.0ඵ͔Β1.5ඵʹऩ·ΔΑ͏ʹ

Slide 19

Slide 19 text

ߴ͍ٕज़ͱ ͍ܰϑοτϫʔΫ Λ໨ࢦ͕ͯ͠Μ͹ͬͯ·͢

Slide 20

Slide 20 text

ࠓ೔ͷྲྀΕ 1. JavaScript ΛͱΓ͍͋ͣ৮ͬͯΈΔ 2. JavaScript Ͱ؆୯ͳΞϓϦέʔγϣϯΛ
 ࡞ͬͯΈΔ

Slide 21

Slide 21 text

৮ͬͯΈΑ͏

Slide 22

Slide 22 text

ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ

Slide 23

Slide 23 text

$ node -v v11.10.1 $ node > // ͜͜ͰJavaScriptΛ࣮ߦ͢Δ͜ͱ͕Ͱ͖·͢ undefined > 1 + 1; 2 > var test = "hoge"; undefined > test 'hoge' > function sample() { ... var num = 5; ... return num; ... } undefined > sample() 5

Slide 24

Slide 24 text

Hello World

Slide 25

Slide 25 text

Hello World

Slide 26

Slide 26 text

$ touch helloworld.js $ code helloworld.js $ node helloworld.js Hello World 01 02 console.log('Hello World');

Slide 27

Slide 27 text

JavaScript ͷ͋Ε͜Ε • Variable declaration • function • object • class • promise

Slide 28

Slide 28 text

Variable declaration

Slide 29

Slide 29 text

Variable declaration είʔϓ ࠶୅ೖ Մมෆม DPOTU MFU WBS

Slide 30

Slide 30 text

Variable declaration είʔϓ ࠶୅ೖ Մมෆม DPOTU ϒϩοΫ ෆՄ Մม MFU ϒϩοΫ Մೳ Մม WBS ؔ਺ Մೳ Մม

Slide 31

Slide 31 text

είʔϓ let a = 1; if (a === 1) { let a = 2; console.log(a); // 2 } console.log(a); // 1 ϒϩοΫ var a = 1; if (a === 1) { var a = 2; console.log(a); // 2 } console.log(a); // 2 ؔ਺ (άϩʔόϧ)

Slide 32

Slide 32 text

࠶୅ೖ ͱ ՄมɺෆՄม ؔ਺ (άϩʔόϧ) const ͸ ࠶୅ೖෆՄೳ const a = 1 a = 2 Uncaught SyntaxError: Identifier 'a' has already been declared const ࠶୅ೖ͕Ͱ͖ͳ͍͚ͩͰॻ͖׵͑ΕΔ const a = [] a.push(1) console.log(a) // [1] a.pop() console.log(a) // []

Slide 33

Slide 33 text

ͲΕ͔ͭ͑͹͍͍ͷʁ • ࠶୅ೖ͸ϩδοΫΛෳࡶԽͤ͞Δ͜ͱ͕ଟ͍
 → جຊతʹ const • Ͳ͏ͯ͠΋࠶୅ೖ͍ͨ͠ͱ͖
 ˠ let • var ͸ lint Ͱېࢭ͢Δ͜ͱ΋ଟ͍

Slide 34

Slide 34 text

function

Slide 35

Slide 35 text

ؔ਺એݴͱؔ਺ࣜ function Greeting() { console.log('hello') } const Greeting = function() {
 console.log('hello') } • syntax ͸Α͘ࣅ͍ͯΔ • એݴ͸ ר্͖͕͛ى͖Δ

Slide 36

Slide 36 text

ؔ਺ࣜͱΞϩʔϑΝϯΫγϣϯ const Greeting = function() { console.log('hello') } const Greeting = () => { console.log('hello') } • ୹͔͚͘Δ • ΞϩʔϑΝϯΫγϣϯ͸ this Λଋറ͠ͳ͍ ؔ਺ࣜͱΞϩʔϑΝϯΫγϣϯͷҧ͍ ୹͔͚͘ΔͷͰΞϩʔϑΝϯΫγϣϯΛ࢖͏৔໘͸ଟ͍

Slide 37

Slide 37 text

JavaScript ʹ͓͚Δ this • Ξϩʔؔ਺Ҏ֎ͷؔ਺ • Ϋϥε • ΦϒδΣΫτ • bindؔ਺ thisͷίϯςΩετ͕มΘΔ৔໘ ҰൠతͳݴޠͱUIJT͕ҧ͏ͱݴΘΕΔͷ͸ UIJTͷίϯςΩετ͕มΘͬͯ͠·͏͔Β https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/this

Slide 38

Slide 38 text

ΞϩʔϑΝϯΫγϣϯ const foo = (a, b) => { return a + b } ΞϩʔϑΝϯΫγϣϯͰ͸͕ࣜҰͭͷͱ͖ʹݶΓϒϩοΫΛলུ͔͚ͯ͠Δ const foo = (a, b) => a + b // ·ͨ͸ const foo = (a, b) => ( a + b
 ) ϒϩοΫΛলུͨ͠৔߹ɼͦͷࣜΛ࣮ߦͨ݁͠Ռ͕ return ͞ΕΔ

Slide 39

Slide 39 text

Ҿ਺ function doSomeThing(a, b, c = 0) {
 console.log('a %o', a) console.log('b %o', b) console.log('c %o', c) } doSomeThing(1, 2) // a 1 // b 2 // c 0 doSomeThing(...[1, 2, 3]) // a 1 // b 2 // c 3 function doSomeThing(...args) {
 console.log('args %o', args) } doSomeThing(1, 2, 3) // args [1, 2, 3] doSomeThing([1], 2, 3) // args [[1], 2, 3] doSomeThing(...[1, 2, 3]) // args [1, 2, 3] σϑΥϧτҾ਺ Մม௕

Slide 40

Slide 40 text

object

Slide 41

Slide 41 text

Objectͱ͸ • σʔλͱػೳͷू߹ • ϓϩύςΟͱϝιου͕ઃఆͰ͖Δ

Slide 42

Slide 42 text

ྫ const person = { name: ['Bob', 'Smith'], age: 32, gender: 'male', interests: ['music', 'skiing'], bio() { alert(`${this.name.join(' ')} is ${this.age} years old.`); }, greeting() { alert('Hi! I\'m ' + this.name[0] + '.'); } };

Slide 43

Slide 43 text

objectʹ͓͚Δmethod const person = { greeting() { alert('Hi!'); } }; const person = { greeting: function() { alert('Hi!'); } }; • ӈͷํ͕୹͔͚͘Δ • ҙຯ͸શ͘ಉ͡

Slide 44

Slide 44 text

const person = { _age: 25, _name: 'maxmellon', get age() { return this._age; }, get name() { return this._name; } }; ObjectͰ΋ήολʔ΍ηολʔ͕࢖͑Δ • private ͕ͳ͔ͬͨ࣌୅͸
 ͜͏͍͏ײ͡ͰΧϓηϧԽͯͨ͠

Slide 45

Slide 45 text

Object Λ࡞Δ const age = 23 const name = 'Kento TSUJI' const parson = { age: age, name: name, } ͜Ε͸ ͜͏͔͚Δ const age = 23 const name = 'Kento TSUJI' const parson = { age, name, }

Slide 46

Slide 46 text

Object ͔Β஋ΛऔΓग़͢ const age = parson.age const name = parson.name ͜Ε͸ const { age, name } = parson ͜͏͔͚Δ

Slide 47

Slide 47 text

Object Λίϐʔ͢Δ const copied = { ...parson } const copied = Object.assign({}, parson) Shallow Copy جຊతʹͬͪ͜ͷॻ͖ํͰॻ͘ Object ͕ωετͨ͠ͱ͖ɼ ࢠͷObjectͷࢀর͸มԽ͠ͳ͍͜ͱʹ஫ҙ ͜ΕΛάάΔͱ͖͸ `spread operator` ͱ͔ εϓϨουԋࢉࢠͱ͔

Slide 48

Slide 48 text

Object ΛϚʔδ͢Δ const a = { foo: 1, hoge: 'a', } const b = { hoge: 'b', poge: 2, } const merged = { ...a, ...b } { foo: 1, hoge: 'b', poge: 2, } ϚʔδͰ΋ `spread operator` ͕࢖͑Δ

Slide 49

Slide 49 text

class

Slide 50

Slide 50 text

ΫϥεએݴͱΫϥεࣜ class Hoge { method() { 
 } } const Hoge = class { method() { } } • Ϋϥεએݴ͸ ר্͖͕͛ى͖ͳ͍ const Hoge = class Hoge { method() { } } Ϋϥεએݴ Ϋϥεࣜ ໊લ෇͖Ϋϥεࣜ

Slide 51

Slide 51 text

class Rectangle { constructor(height, width) { this.height = height; this.width = width; } // ήολʔ get area() { return this.calcArea(); } // ϝιου calcArea() { return this.height * this.width; } } const square = new Rectangle(10, 10); console.log(square.area); // 100 square.height = 20; // 'aaaa' https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes ϝιουఆٛ

Slide 52

Slide 52 text

Tips • JavaScript͸prototypeϕʔεͷݴޠ • class ͸ prorotypeͱObjectΛ࢖ͬͨ
 දݱΛ؆ܿʹΘ͔Γ΍͘͢ॻͨ͘Ίͷ΋ͷ • ͳͷͰ͍ΘΏΔΠϯελϯεϝιου͸
 AnyClass.ptototype.method ͷΑ͏ʹදݱ͢Δ
 ↑ ͜Ε͸ new AnyClass().method() • ελςΟοΫؔ਺͸AnyClass.staticMethod

Slide 53

Slide 53 text

༨ஊ private class field

Slide 54

Slide 54 text

Ϟμϯͳϒϥ΢βͰ͸private field ͕࢖͑Δ • ϨΨγʔϒϥ΢βͰ࢖͏ʹ͸ transpile ͳͲͷϓϦϓϩηε͕ඞཁ https://github.com/tc39/proposal-private-fields/blob/master/OLD_README.md class Point { #x = 0; #y = 0; constructor(x = 0, y = 0) { this.#x = +x; this.#y = +y; } get x() { return this.#x } set x(value) { this.#x = +value } get y() { return this.#y } set y(value) { this.#y = +value } }

Slide 55

Slide 55 text

Ϟμϯͳϒϥ΢βͰ͸private field ͕࢖͑Δ • ϨΨγʔϒϥ΢βͰ࢖͏ʹ͸ transpile ͳͲͷϓϦϓϩηε͕ඞཁ https://github.com/tc39/proposal-private-fields/blob/master/OLD_README.md class Point { #x = 0; #y = 0; constructor(x = 0, y = 0) { this.#x = +x; this.#y = +y; } get x() { return this.#x } set x(value) { this.#x = +value } get y() { return this.#y } set y(value) { this.#y = +value } }

Slide 56

Slide 56 text

ಉظ / ඇಉظ

Slide 57

Slide 57 text

΋͠ඇಉظ͕ͳ͔ͬͨΒͲ͏ͳΔ͔ • αʔόʔ͔ΒσʔλΛड͚औΔ·Ͱ
 ଞͷૢ࡞͕Ͱ͖ͳ͍ • ΫϦοΫ͢Δͱϒϥ΢β͕ݻ·Δ • ॲཧ͕Ұ͔ͭͣͭ͠Ͱ͖ͳ͍

Slide 58

Slide 58 text

callbackʹΑΔඇಉظ taskA(() => { taskB(() => { taskC(() => { … }) }) }) ͲΜͲΜωετ͕ਂ͘ͳ͍ͬͯͬͯ͠·͏

Slide 59

Slide 59 text

promise

Slide 60

Slide 60 text

promise new Promise((resolve, reject) => { asynchronizedTask((err, result) => { if (err) reject(err) resolve(result) }) }).then(res => console.log(res)) .catch(err => console.error(err)) • ඇಉظॲཧͷந৅Խ • then Ͱ ੒ޭͨ͠ͱ͖ͷϋϯυϥΛ ෇༩͢Δ ৽͍͠PromiseΛฦ͢
 (ݫີʹ͸ࣦഊϋϯυϥ΋௥ՃͰ͖Δʣ • catch Ͱ ࣦഊϋϯυϥͷίʔϧόοΫΛ෇༩͢Δ

Slide 61

Slide 61 text

taskA(() => { taskB(() => { taskC(() => { … }) }) }) taskA() .then(() => taskB()) .then(() => taskC()) callbackΑΓ΋Θ͔Γ΍͘͢ॻ͘͜ͱ͕Ͱ͖Δ

Slide 62

Slide 62 text

promise QSPNJTF UIFO IBOEMFS UIFO IBOEMFS DBUDI PO3FKFDUJPO FSSPSIBOEMJOH QSPNJTF UIFO IBOE UIFO IBOE DBUDI PO3FKF ଴ػঢ়ଶ ੒ޭ ࣦഊ promise͕ࣦഊͨ͠ͱ͖
 thenͷୈೋҾ਺ͷhandlerͰྫ֎͕ग़ͨͱ͖ return return ੒ޭ ࣦഊ thenͷϋϯυϥ͸ඞͣPromiseΛฦ͢ͷͰ chainͰ͖Δ chainͰ͖Δ

Slide 63

Slide 63 text

example new Promise((resolve) => { // 1ඵޙʹ ୈ1Ҿ਺ͷؔ਺Λ࣮ߦ͢Δ setTimeout(() => resolve(1), 1000) }).then((res) => {
 console.log(res)
 return res + 1 }).then((res) => { console.log(res)
 return res + 1
 }).then((res) => { console.log(res)
 return res + 1
 }) ࣮ߦ͢ΔͱͲΜͳϩάग़ྗ͞ΕΔʁ

Slide 64

Slide 64 text

fetch API const url = 'https://hacker-news.firebaseio.com/v0/item/2921983.json? print=pretty' fetch(url) .then(resp => resp.json()) .then(resp => console.log(resp)) • ྫ͑͹ɼhacker news ͷ API Λୟ͘ͱ͢Δ { "by" : "norvig", "id" : 2921983, "kids" : [ 2922097, 2922429, 2924562, 2922709, 2922573, 2922140, 2922141 ], "parent" : 2921506, "text" : "Aw shucks, guys ... you make me blush with your compliments.

Tell you what, Ill make a deal: I'll keep writing if you keep reading. K?", "time" : 1314211127, "type" : "comment" }

Slide 65

Slide 65 text

async / await

Slide 66

Slide 66 text

async function function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { resolve('resolved'); }, 2000); }); } async function asyncCall() { console.log('calling'); const result = await resolveAfter2Seconds(); console.log(result); // expected output: 'resolved' } ಉظؔ਺ͷΑ͏ʹPromiseΛѻ͏

Slide 67

Slide 67 text

const doSomeThing = () => {
 taskA() .then(() => taskB()) .then(() => taskC()) } callbackΑΓ΋Θ͔Γ΍͘͢ॻ͘͜ͱ͕Ͱ͖Δ const doSomeThing = async () => { await taskA() await taskB() await taksC() }

Slide 68

Slide 68 text

ԋश

Slide 69

Slide 69 text

४උ $ npm install @recruit-tech/javascripting -g $ javascripting ↑ ͜ΕΛ࣮ߦ ↓ Έ͍ͨͳը໘͕ͰΕ͹ OK

Slide 70

Slide 70 text

೔ຊޠ΁ͷม͑ํ

Slide 71

Slide 71 text

ΨϯΨϯ ਐΊͪΌ͍ͬͯͩ͘͞ (1h ΄ͲऔΓ·͢)

Slide 72

Slide 72 text

ޙ൒

Slide 73

Slide 73 text

ԋश 1. JavaScript ΛͱΓ͍͋ͣ৮ͬͯΈΔ 2. JavaScript Ͱ؆୯ͳΞϓϦέʔγϣϯΛ
 ࡞ͬͯΈΔ

Slide 74

Slide 74 text

ࠔͬͨͱ͖ • ҰൠతͳهࣄͷલʹυΩϡϝϯτΛಡΉบΛ
 ͚ͭ·͠ΐ͏ • ΋͠OSSΛར༻͍ͯ͠ΔͷͰ͋Ε͹ɺ
 ιʔείʔυ΋ಡΉΑ͏ʹҙࣝ͠·͠ΐ͏ • ࠓճͷ৔߹ɺMDN͕࠷΋ࢀߟʹͳΔͱ
 ͓΋͍·͢

Slide 75

Slide 75 text

ex. JavaScript ͔Β DOMΛ࡞Γ͍ͨ

Slide 76

Slide 76 text

͜͜Ͱ࢖͑ͦ͏ͳAPI • Document.createElement
 https://developer.mozilla.org/ja/docs/Web/API/Document/createElement • Document.querySelector
 https://developer.mozilla.org/ja/docs/Web/API/Document/querySelector • Array.prototype.forEach
 https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach • Array.prototype.map
 https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Slide 77

Slide 77 text

JavaScript͔ΒDOMΛૢ࡞͢Δ • DOMπϦʔͷ࡞੒ํ๏
 https://developer.mozilla.org/ja/docs/Web/API/Document_Object_Model/How_to_create_a_DOM_tree

Slide 78

Slide 78 text

0. ༻ҙ • https://github.com/MaxMEllon/todo-mvc/ Λfork • git clone • npm install • npm start • ϒϥ΢βͰ http://localhost:3000 ʹΞΫηεʂ

Slide 79

Slide 79 text

1. APIΛઃܭ͢Δ • create (GET) read (POST) update (PATCH) delete (DELETE) ͷ4ͭ • read ͸ TODO Λશ݅औಘ • TODO ͸ ໊લͱ׬͔ྃͨ͠
 Ͳ͏͔ͷεςʔλε͔Βߏ੒ ͻͱ·ͣ Read ͚ͩ Agreed Λ࢖ͬͯ ઃܭͯ͠ΈΑ͏

Slide 80

Slide 80 text

2. API͔ΒTODOҰཡΛඳը͢Δ ͖ͬ͞࡞ͬͨ
 agreed ͔Βऔಘʂ

Slide 81

Slide 81 text

3. Form͔ΒจࣈྻΛೖྗͯ͠TODOΛ
 ࡞੒Ͱ͖ΔΑ͏ʹ͢Δ

Slide 82

Slide 82 text

4. TODOΛ׬ྃঢ়ଶʹͰ͖ΔΑ͏ʹ͢Δ
 (API௨৴ࠐͰ)

Slide 83

Slide 83 text

5. TODOΛ࡟আͰ͖ΔΑ͏ʹ͢Δ

Slide 84

Slide 84 text

6. TODOͷDONEʹͳͬͨ݅਺Λ਺͑Δ

Slide 85

Slide 85 text

7. Server Λ࣮૷͢Δ

Slide 86

Slide 86 text

Φϓγϣφϧͳ՝୊ • TODOͷ໊લΛฤूͰ͖ΔΑ͏ʹͯ͠ΈΑ͏ • σβΠϯΛม͑ͯΈΑ͏ • MVVM΍FluxͰॻ͖௚ͯ͠ΈΑ͏ • WebComponents Ͱॻ͖௚ͯ͠ΈΑ͏