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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Araya
August 20, 2018
Programming
0
280
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.6k
JavaScript Promise API in 2019
arayaryoma
1
860
gotandajs-11-nodejs-recursive-readdir
arayaryoma
2
510
What about Temporal in JavaScript
arayaryoma
5
1.6k
2018-07-31-angular-tips
arayaryoma
1
130
Other Decks in Programming
See All in Programming
AI巻き込み型コードレビューのススメ
nealle
2
340
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
980
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
170
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
190
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
200
CSC307 Lecture 02
javiergs
PRO
1
780
Claude Codeと2つの巻き戻し戦略 / Two Rewind Strategies with Claude Code
fruitriin
0
120
ノイジーネイバー問題を解決する 公平なキューイング
occhi
0
100
Data-Centric Kaggle
isax1015
2
780
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.9k
2026年 エンジニアリング自己学習法
yumechi
0
140
Featured
See All Featured
Test your architecture with Archunit
thirion
1
2.2k
A Modern Web Designer's Workflow
chriscoyier
698
190k
How STYLIGHT went responsive
nonsquared
100
6k
Embracing the Ebb and Flow
colly
88
5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
93
Site-Speed That Sticks
csswizardry
13
1.1k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
260
Color Theory Basics | Prateek | Gurzu
gurzu
0
200
Chasing Engaging Ingredients in Design
codingconduct
0
110
Amusing Abliteration
ianozsvald
0
100
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7k
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ͱ͔Γ͡ΊΔͱશʹผݴޠ