Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
var, let, const and immutable of JavaScript
Search
Araya
August 20, 2018
Programming
300
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
var, let, const and immutable of JavaScript
JavaScriptのvar, let, constによる変数宣言、immutableについて、社内LTで話しました。
Araya
August 20, 2018
More Decks by Araya
See All by Araya
Performance and cache strategy at NIKKEI
arayaryoma
0
2.8k
JavaScript Promise API in 2019
arayaryoma
1
880
gotandajs-11-nodejs-recursive-readdir
arayaryoma
2
530
What about Temporal in JavaScript
arayaryoma
5
1.7k
2018-07-31-angular-tips
arayaryoma
1
140
Other Decks in Programming
See All in Programming
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
500
リアルな遅延を測る仕様
kota_yata
1
110
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
340
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
560
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
420
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
220
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
550
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
230
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
300
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
640
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
210
Featured
See All Featured
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
470
Six Lessons from altMBA
skipperchong
29
4.4k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
490
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
800
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
860
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
370
We Have a Design System, Now What?
morganepeng
55
8.3k
Transcript
ઌͣconstΑΓ࢝ΊΑ @arayaryoma 2018/08/20 - ϔϚλΠτࣾLT
ٱʑʹJavaScriptॻ͚͘ͲES2015ͱ͔ ͍͏ͭͰ͍Ζ͍ΖมΘͬͯΔͬΆ͍ʁ มએݴ࣌ɺvar ͚ͩ͡Όͳͯ͘let͑Δʁ varมͩ͠letఆͬΆ͍…ʁ Μɺconst͋Δʁ ͔ͯ͠͠ࢲͷJavaScriptɺ3ΕͯΔʁ
ٱʑʹJavaScriptॻ͚͘ͲES2015ͱ͔ ͍͏ͭͰ͍Ζ͍ΖมΘͬͯΔͬΆ͍ʁ มએݴ࣌ɺvar ͚ͩ͡Όͳͯ͘let͑Δʁ varมͩ͠letఆͬΆ͍…ʁ Μɺconst͋Δʁ ͔ͯ͠͠ࢲͷJavaScriptɺ3ΕͯΔʁ ࠓͦΜͳ͋ͳͨͷͨΊʹ varͱletͱconstͷҧ͍Λ ղઆ͠·͢
ٱʑʹJavaScriptॻ͚͘ͲES2015ͱ͔ ͍͏ͭͰ͍Ζ͍ΖมΘͬͯΔͬΆ͍ʁ มએݴ࣌ɺvar ͚ͩ͡Όͳͯ͘let͑Δʁ varมͩ͠letఆͬΆ͍…ʁ Μɺconst͋Δʁ ͔ͯ͠͠ࢲͷJavaScriptɺ3ΕͯΔʁ ͜ͷεϥΠυΛݟऴΘΔࠒ ͋ͳͨvarΛॻ͔ͳ͍ମʹ ͳ͍ͬͯΔͰ͠ΐ͏
͢͜ͱͱ݁ JSͷvar, let, constͷҧ͍ͱ͍ॴ ϞμϯͳJSͷॻ͖ํ(ྲྀߦ)ʹ͍ͭͯগ͚ͩ͠ varશʹෆཁ ·ͣconstͰॻ͘
redeclaration(࠶એݴ)
var let const
ಈ͘ʁ
ಈ͘(variable shadowing)
constಉ͡ ಈ͕͘Մಡੑ͕Լ͕ΔͷͰආ͚Δ͖
scope(มείʔϓ)
varͰએݴͨ͠มͷείʔϓfunction (τοϓϨϕϧʹએݴͨ͠߹άϩʔόϧ)
if(){}ͳͲͷϒϩοΫͰએݴͨ͠มΛ ϒϩοΫͷ֎͔ΒࢀরͰ͖Δ
letͰએݴͨ͠มείʔϓ͕ϒϩοΫ
constಉ͘͡ϒϩοΫείʔϓ
switch-caseͷ᠘
blockʹͯ͠ղܾ
reassign(࠶ೖ)
var࠶ೖՄೳ
let࠶ೖՄೳ
const࠶ೖෆՄೳ
ͨͩ͠constͰએݴͯ͠immutableͰͳ͍ͷͰҙɻ ObjectͷpropertyArrayͷཁૉ͍͘ΒͰॻ͖ΘΔɻ
var let const ࠶એݴ είʔϓ function block block ࠶ೖ immutable?
(primitiveͳσʔλܕҎ֎) NO NO NO
࣮ફྫͰݟΔ ݱ෩JavaScript
࣮ફྫ1 Array [1,2,3,4,5]ͷ֤ཁૉΛ10ഒͯ͠ग़ྗ
Bad. i͕for blockͷ֎ʹӨڹΛٴ΅͢
iͷείʔϓ͕for blockʹͳͬͨɻ Ͱ͏ͪΐͬͱ؆ܿʹॻ͖͍ͨɻ arrͷཁૉΛॻ͖͑ͯΔͷා͍ɻ
࣮1ߦ arrΛมߋ͠ͳ͍ɻGood
࣮ફྫ2 Array [1,2,3,4,5]ͷ֤ཁૉͷ߹ܭΛग़ྗ
letͱfor loop
for-ofΛ͏
Array.prototype.reduceΛ͏ɻ sumΛ࠶ೖෆՄʹͰ͖ͯ҆શɻ
ݱ෩JS มείʔϓͰ͖Δ͚ͩখ͘͞ɺͰ͖Δ͚ͩ࠶ೖෆՄʹ͢Δ Ұ࡞ͬͨobjectมԽͤ͞ͳ͍ͷ͕ਖ਼ٛ ෭࡞༻ͷൃੜΛආ͚Δ Immutable.jsΛͬͯcollectionͯ͢immutableͰ͋Δ͜ͱΛલఏʹ͢Δ ྲྀ ArrayͳͲʹศརͳmethod༷ͰՃ͞Ε͍ͯΔ(https://github.com/tc39/ proposals)
࣮ફྫ3 ඇಉظ+varͷා͞Λମݧ(ϥΠϒίʔσΟϯά)
ArrayObjectimmutableʹ͍ͨ͠ਓ
Object.freeze()ͰखܰʹObjectArrayΛimmutableʹ
Immutable.jsͷclassΛ͏
Immutable.jsͬͯʁ https://facebook.github.io/immutable-js/ WebϑϩϯτΤϯυͰ͓ೃછΈͷFacebook JavaScriptͰimmutableͳܕΛѻ͑ΔΑ͏ʹ͢ΔͨΊͷϥΠϒϥϦ ObjectArrayͳͲɺJavaScriptͷbuilt-inͳσʔλܕΛimmutableʹ ͢ΔΞϓϩʔνͰͳ͘ɺؔܕݴޠ͔Β༌ೖͨ͠ಠࣗͷσʔλܕ Λѻ͏ List, Stack, Map,
OrderedMap, Set, OrderedSet,
·ͱΊ var͏͍Βͳ͍ ·ͣconstͰॻ͍ͯɺͲ͏ͯ͠ඞཁͳΒletΛ͏ ෭࡞༻Λආ͚Δ͜ͱ͕ਖ਼ٛͩͱ͍͏࣌ ؔܕݴޠͷύϥμΠϜ͕JSͷੈքʹ༌ೖ͞ΕͯΔ TypeScript + Rx.js + Immutable.jsͱ͔Γ͡ΊΔͱશʹผݴޠ