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
470
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
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
880
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
290
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
テストコード書いてみませんか?
onopon
2
340
AHC041解説
terryu16
0
410
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
950
return文におけるstd::moveについて
onihusube
1
1.4k
Alba: Why, How and What's So Interesting
okuramasafumi
0
210
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
170
functionalなアプローチで動的要素を排除する
ryopeko
1
220
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
870
Typedesign – Prime Four
hannesfritz
40
2.5k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
Why Our Code Smells
bkeepers
PRO
335
57k
Building Adaptive Systems
keathley
38
2.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
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ͱ͔Γ͡ΊΔͱશʹผݴޠ