-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day14_30DayJS Array and Object reference vs copy
- Loading branch information
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<title>JS Reference VS Copy</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
|
||
<body> | ||
<div class="navigation"> | ||
<a class="card previous-card" href="https://archeana.github.io/JS30/Day_13/index.html" | ||
onclick="goToPreviousProject()"> | ||
<span>Previous</span> | ||
</a> | ||
<a class="card home-card" href="https://archeana.github.io/JS30/" onclick="goToHomepage()"> | ||
<span>Home</span> | ||
</a> | ||
<a class="card next-card" href="#" onclick="goToNextProject()"> | ||
<span>Next</span> | ||
</a> | ||
</div> | ||
<br> | ||
<p><em>Psst: have a look at the JavaScript Console or just skip to the next project</em> 💁</p> | ||
<script> | ||
// start with strings, numbers and booleans | ||
// let age = 100; | ||
// let age2 = age; | ||
// console.log(age, age2); | ||
// age = 200; | ||
// console.log(age, age2); | ||
// Let's say we have an array | ||
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; | ||
|
||
|
||
// and we want to make a copy of it. | ||
const team = players; | ||
|
||
console.log(players, team); | ||
|
||
// You might think we can just do something like this: | ||
// team[5] = 'Lux'; | ||
// however what happens when we update that array? | ||
|
||
// now here is the problem! | ||
|
||
// oh no - we have edited the original array too! | ||
|
||
// Why? It's because that is an array reference, not an array copy. They both point to the same array! | ||
|
||
// So, how do we fix this? We take a copy instead! | ||
const team2 = players.slice(); | ||
// one way | ||
|
||
// or create a new array and concat the old one in | ||
const team3 = [].concat(players); | ||
|
||
// or use the new ES6 Spread | ||
const team4 = [...players]; | ||
team4[3] = 'heeee hawww'; | ||
console.log(team4); | ||
|
||
const team5 = Array.from(players); | ||
// now when we update it, the original one isn't changed | ||
|
||
// The same thing goes for objects, let's say we have a person object | ||
|
||
// with Objects | ||
const person = { | ||
name: 'Wes Bos', | ||
age: 80 | ||
}; | ||
|
||
// and think we make a copy: | ||
|
||
// how do we take a copy instead? | ||
const cap2 = Object.assign({}, person, { number: 99, age: 12 }); | ||
console.log(cap2); | ||
|
||
// We will hopefully soon see the object ...spread | ||
|
||
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it. | ||
|
||
const wes = { | ||
name: 'Wes', | ||
age: 100, | ||
social: { | ||
twitter: '@wesbos', | ||
facebook: 'wesbos.developer' | ||
} | ||
}; | ||
|
||
console.clear(); | ||
console.log(wes); | ||
// this is how we copy- assign uses one level and not all of them | ||
const dev = Object.assign({}, wes); | ||
// this how you can clone (he does not recomend) | ||
const dev2 = JSON.parse(JSON.stringify(wes)); | ||
|
||
</script> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
html { | ||
box-sizing: border-box; | ||
} | ||
|
||
*, *:before, *:after { | ||
box-sizing: inherit; | ||
} | ||
|
||
|
||
|
||
|
||
body { | ||
text-align: center; | ||
background: #193549; | ||
color: white; | ||
font-family: 'helvetica neue', sans-serif; | ||
font-weight: 100; | ||
font-size: 1.4em; | ||
position: relative; | ||
} | ||
|
||
|
||
|
||
.navigation { | ||
color: white; | ||
font-family: 'helvetica neue', sans-serif; | ||
position: fixed; | ||
top: 20px; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
z-index: 1000; | ||
} | ||
|
||
.card-back{ | ||
left: auto; | ||
} | ||
|
||
.card { | ||
width: 100px; | ||
height: 50px; | ||
margin: 5px; | ||
font-size: 20px; | ||
background-color: #A2748B; | ||
color: #ffffff; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
overflow: hidden; | ||
transition: 1s ease; | ||
text-align: center; | ||
align-items: center; | ||
justify-content: space-between; | ||
line-height: 16px; | ||
display: flex; | ||
justify-content: center; | ||
position: relative; | ||
} |