海外TECH |
DEV Community |
Javascript Shallow Copy - what is a Shallow Copy? |
https://dev.to/smpnjn/javascript-shallow-copy-what-is-a-shallow-copy-1pc5
|
Javascript Shallow Copy what is a Shallow Copy Shallow copy and deep copy are terms thrown around in Javascript that can be confusing if you have never heard them before It is quite common to hear that array methods like slice or filter make a shallow copy of the original array What is a shallow copy in JavaScript A shallow copy of an arrays or object is one where they both have the same reference in memory That means that if you change the shallow copy it may change the original copy too I say may since that is not always the case Let s look at an example using slice let arrayOne ️ let arrayOneSlice arrayOne slice console log arrayOne ️ console log arrayOneSlice Here we have an array which we then slice in the variable arrayOneSlice Both of these arrays have the same reference in memory since slice makes a shallow copy of them So if we try to update arrayOneSlice it will affect arrayOne too right let arrayOne ️ let arrayOneSlice arrayOne slice Update arrayOneSlicearrayOneSlice ️ console log arrayOne ️ console log arrayOneSlice empty ️ Only it doesn t since we used the square bracket notation Javascript interprets this as putting a new value into the position So only arrayOneSlice is updated and for good reason too Although is at position in arrayOne it is in position in arrayOneSlice This can give the illusion that these two arrays are copies and act independently of each other but that s not the case either Consider the following example let arrayOne items let arrayOneSlice arrayOne slice Update arrayOneSlicearrayOneSlice items ️ console log arrayOne items ️ console log arrayOneSlice items ️ Here we updated arrayOneSlice items and its updated on both arrays since items exists on both arrays at the same position and we didnt assign a new value but rather used the dot notation to update an existing property In Javascript this updates both the original and the copy we made using slice The main point to remember with shallow copies is that adjusting one can affect the original you are trying to copy the reference in memory is the same and the reference points to the values of the array so you have to be more careful What you don t want to do is create unexpected behaviour where the original and copy of an array do not update in sync when you expected them to So how do you make deep copies in Javascript Javascript has historically had a bit of a problem with deep copies Most methods in Javascript like the three dots syntax Object create Object assign and Array from all make shallow copies Deep copies have different references in memory though so you don t have to worry about mutating the original when using them This makes them very useful when we want to avoid that Deep copies can be made via serialisation or a custom script to copy each part of an object or an array into a new one creating a new reference in memory For example this will create a new array in Javascript with a new reference let myArray let deepCopy JSON parse JSON stringify myArray You can also use the structuredClone function to make a deep copy let myArray let deepCopy structuredClone myArray Now we have created new arrays with deep copies we no longer need to worry about messing up the original when we change the copy ConclusionShallow copies are quite confusing and one of the many quirks which Javascript presents Understanding what they are can save you a lot of headaches when debugging in the future and using deep copies is a good way to avoid some of these issues |
2022-10-22 19:43:42 |
Apple |
AppleInsider - Frontpage News |
Early Black Friday sale at Amazon drives iPad Pro & iPad Air prices down to record low levels |
https://appleinsider.com/articles/22/10/22/early-black-friday-sale-at-amazon-drives-ipad-pro-ipad-air-prices-down-to-record-low-levels?utm_medium=rss
|
Early Black Friday sale at Amazon drives iPad Pro amp iPad Air prices down to record low levelsSave up to on Apple s M iPad Pro inch and iPad Air with early Black Friday discounts matching and even beating in some cases record low prices Save up to on Apple iPads The weekend sale at Amazon discounts the M iPad Pro inch with GB of storage and Wi Fi connectivity to matching the previous record low price found during the Prime Early Access Sale Considering Apple announced the M iPad Pro line this week inventory may be limited on the now closeout M models Read more |
2022-10-22 19:14:32 |
海外TECH |
Engadget |
Microsoft is making it easier for Xbox users to join Discord voice chats |
https://www.engadget.com/microsoft-is-making-joining-discord-voice-chat-easier-191055133.html?src=rss
|
Microsoft is making it easier for Xbox users to join Discord voice chatsMicrosoft and Discord plan to make joining a voice channel from an Xbox console easier If you re a frequent Discord user you may recall the companies recently launched Xbox One and Xbox Series X S integration Following a beta test in July Discord started rolling out the feature to all Xbox users in September Unfortunately in its current iteration the integration isn t as straightforward as one would hope joining a voice chat involves a transfer process that requires the Xbox mobile app That s about to change In a blog post spotted by The Verge Microsoft announced it was removing the need to use a phone to join Discord voice chats as part of a beta update Xbox Insiders can try out right now The new software adds a server browser that allows you to join a voice channel directly from your console You ll still need your phone handy if you want to call a specific friend over Discord but the update otherwise simplifies what was an overly complicated process previously While the feature is currently in beta Microsoft will likely roll it out to all Xbox users sometime over the next few weeks |
2022-10-22 19:10:55 |
海外TECH |
WIRED |
In 'Synchronic,' Time Travel Is Anything but Nostalgic |
https://www.wired.com/2020/10/geeks-guide-synchronic/
|
romanticize |
2022-10-22 19:25:25 |
ニュース |
BBC News - Home |
Boris Johnson, Rishi Sunak and Penny Mordaunt: Who are MPs backing? |
https://www.bbc.co.uk/news/uk-politics-63343308?at_medium=RSS&at_campaign=KARANGA
|
minister |
2022-10-22 19:32:58 |
ニュース |
BBC News - Home |
Chelsea 1-1 Man Utd: Hosts remain above United after dramatic finale |
https://www.bbc.co.uk/sport/football/63267141?at_medium=RSS&at_campaign=KARANGA
|
league |
2022-10-22 19:20:34 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
Zoomやツイッターがもたらす「脳機能&記憶」への影響とは?脳専門医が伝えたい“意外な罠” - from AERAdot. |
https://diamond.jp/articles/-/311492
|
fromaeradot |
2022-10-23 04:50:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
東京で癒しと開運の旅行プラン!アニマルカフェ、縁結び神社、縁起物スイーツ… - 地球の歩き方ニュース&レポート |
https://diamond.jp/articles/-/311486
|
東京で癒しと開運の旅行プランアニマルカフェ、縁結び神社、縁起物スイーツ…地球の歩き方ニュースレポート海外旅行ガイドブックの決定版『地球の歩き方』から、今回紹介する記事は「東京で癒しと開運の旅行プラン」です。 |
2022-10-23 04:45:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
腰痛予防も姿勢改善もできる「筋トレ3選」、四つんばいで15分が腹筋に効く - 男のオフビジネス |
https://diamond.jp/articles/-/311189
|
四つんばい |
2022-10-23 04:40:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
新日本酒紀行「辻善兵衛」 - 新日本酒紀行 |
https://diamond.jp/articles/-/310718
|
真岡鐵道 |
2022-10-23 04:35:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
糖尿病の治療で「クマの冬眠」に注目集まる、米国での研究結果とは - ヘルスデーニュース |
https://diamond.jp/articles/-/311633
|
糖尿病の治療で「クマの冬眠」に注目集まる、米国での研究結果とはヘルスデーニュースクマは冬に入る前、大量に食べて体重を増やし、そのあと数カ月間は横になったまま動かないというパターンを毎年繰り返しているが、糖尿病にならない。 |
2022-10-23 04:30:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
「投資で資産を増やせる人」と「結局損をする人」の決定的な差とは? - 投資家の思考法 |
https://diamond.jp/articles/-/311193
|
thesalon |
2022-10-23 04:25:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
「死ぬほど嫌いなやつ」のたった1つの対処法 - 99%はバイアス |
https://diamond.jp/articles/-/311364
|
突破 |
2022-10-23 04:20:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
【マンガ】肛門は「実弾と空砲」を瞬時に区別するという驚くべき事実 - すばらしい人体 |
https://diamond.jp/articles/-/311546
|
【マンガ】肛門は「実弾と空砲」を瞬時に区別するという驚くべき事実すばらしい人体累計万部突破唾液はどこから出ているのか、目の動きをコントロールする不思議な力、人が死ぬ最大の要因、おならはなにでできているか、「深部感覚」はすごい…。 |
2022-10-23 04:15:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
【今すぐ逃げて】「他人のメンタルを消耗させる人」を見分ける方法 - だから、この本。 |
https://diamond.jp/articles/-/311496
|
今回は本書の発売を記念し、人生の苦境を乗り越えるためのヒントを、野沢さんにもらうことにしました。 |
2022-10-23 04:10:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
【「正解がない時代」の思考法】 「絵の具を塗りたくっただけのような絵」が芸術史に残る傑作になった理由 - 良書発見 |
https://diamond.jp/articles/-/310890
|
|
2022-10-23 04:05:00 |
ビジネス |
東洋経済オンライン |
湘南モノレール「ピンクの車両」が担う重大な使命 乳がん撲滅「ピンクリボン運動」を1年中啓発 | ローカル線・公共交通 | 東洋経済オンライン |
https://toyokeizai.net/articles/-/627023?utm_source=rss&utm_medium=http&utm_campaign=link_back
|
東洋経済オンライン |
2022-10-23 04:30:00 |
コメント
コメントを投稿