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
0
230
var, let, const and immutable of JavaScript
JavaScriptのvar, let, constによる変数宣言、immutableについて、社内LTで話しました。
Araya
August 20, 2018
Tweet
Share
More Decks by Araya
See All by Araya
Performance and cache strategy at NIKKEI
arayaryoma
0
2.3k
JavaScript Promise API in 2019
arayaryoma
1
800
gotandajs-11-nodejs-recursive-readdir
arayaryoma
2
460
What about Temporal in JavaScript
arayaryoma
5
1.5k
2018-07-31-angular-tips
arayaryoma
1
120
Other Decks in Programming
See All in Programming
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
200
Refactor your code - refactor yourself
xosofox
1
250
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
200
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Writing Fast Ruby
sferik
628
61k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Designing for humans not robots
tammielis
250
25k
Navigating Team Friction
lara
183
15k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
94
The Language of Interfaces
destraynor
154
24k
KATA
mclloyd
29
14k
Designing for Performance
lara
604
68k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Making Projects Easy
brettharned
116
5.9k
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ͱ͔Γ͡ΊΔͱશʹผݴޠ