Skip to content

Commit 7db7eaa

Browse files
committed
>challenge 14 documented + updated root readme
1 parent 897ada7 commit 7db7eaa

File tree

4 files changed

+96
-153
lines changed

4 files changed

+96
-153
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Exercise 14: JavaScript Reference vs Copying
2+
Nitish Dayal, Software & Applications Developer - [Contact](http://nitishdayal.me)
3+
Last Commit Date: Dec 19, 2016
4+
5+
No guide necessary! We're learning about JavaScript variable referencing vs.
6+
copying. If you want a full-blown explanation, take a look at [chapter 11, section
7+
2 of 'JavaScript: The Definitive Guide'](http://docstore.mik.ua/orelly/webprog/jscript/ch11_02.htm).
8+
The easiest way I can summarize the information is that _primitive types are
9+
manipulated by value_ and _object types are manipulated by reference_. Still confused?
10+
Have **NO** idea what I'm talking about? Carry on, my wayward son; there'll be peace
11+
when you are done.
12+
13+
## Manipulating by Value
14+
15+
**Primitive types** are _manipulated by value_. The following types are considered
16+
**primitive types** in JavaScript:
17+
- String
18+
- Number
19+
- Boolean
20+
- Null
21+
- Undefined
22+
23+
This means that if we define a variable as a **primitive type**, and then define
24+
_another variable_ as the previously defined variable, the second variable
25+
will _copy the current value of the first variable_. Any changes to the first
26+
variable will not effect the second, and vice versa.
27+
28+
Example:
29+
30+
```JavaScript
31+
let me = "Nitish"
32+
let me2 = me
33+
console.log(me === me2) // true
34+
35+
me2 = "Bob Dylan"
36+
console.log(me === me2, me, me2) // false, "Nitish", "Bob Dylan"; I'm not Bob Dylan
37+
38+
me = "Not Nitish"
39+
console.log(me === me2, me, me2) // false, "Not Nitish", "Bob Dylan"
40+
```
41+
42+
## Manipulating by Reference
43+
44+
**Object types** are _manipulated by reference_. If it's not a primitive type, it's
45+
**always** an object. If we're being really technical, almost everything _can be_
46+
an object in JavaScript, even primitive types (excluding `null` and `undefined`), but
47+
let's try not to get hung up on technicalities.
48+
49+
A small list of **object types** in JavaScript:
50+
- Object
51+
- Function
52+
- Array
53+
- Set
54+
55+
Let's say we declare a variable and define it as an object, then declare _another_
56+
variable and define it as the first variable:
57+
58+
```JavaScript
59+
const me = {name: "Nitish", age: 26}
60+
const me2 = me
61+
console.log(me === me2) // true
62+
```
63+
64+
If we update the property of the object by calling on _either variable_, both variables
65+
will reflect that change.
66+
67+
```JavaScript
68+
me.name = "Not Nitish"
69+
console.log(me === me2) // true
70+
console.log(me2) // { name: 'Not Nitish', age: 26 }
71+
```
72+
73+
This is because `me2` does not copy `me`; it contains a _reference_ to the object
74+
defined in `const me`. Any changes applied directly to the object will have an
75+
effect on all variables referencing that particular object. So what could we do
76+
if we wanted to make a copy of the original object so as not to manipulate the
77+
root source of data?
78+
79+
```JavaScript
80+
const me3 = Object.assign({}, me) // create a new object
81+
console.log(me3) // {name: 'Not Nitish', age: 26}
82+
console.log(me === me3) // false! It's a new object instance!
83+
console.log(me.name === me3.name) // true! The property values are the same!
84+
me3.name = "Devin"
85+
console.log(`${me.name}, ${me3.name}`) // 'Not Nitish, Devin'
86+
```
87+
88+
We have effectively copied the first object and can modify our copy without
89+
manipulating the original.
90+
91+
**WARNING: If we copy an object _containing objects_, we are only copying
92+
the first level. Anything deeper than that will still be a reference.**
93+
94+
![The More You Know!](http://www.d-toolsblog.com/wp-content/uploads/2008/11/the_more_you_know2.jpg)

exercises/14 - JavaScript References VS Copying/index-FINISHED.html

Lines changed: 0 additions & 99 deletions
This file was deleted.

exercises/14 - JavaScript References VS Copying/index-START.html

Lines changed: 0 additions & 52 deletions
This file was deleted.

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# JavaScript30 - 30 Day JavaScript Challenge
22
Nitish Dayal, Software & Applications Developer
3-
Last Commit Date: Dec 18, 2016
3+
Last Commit Date: Dec 19, 2016
44

55
> Course created by [Wes Bos](https://github.com/wesbos)
66
> Join the challenge (for free!) here - [JavaScript30](https://javascript30.com/account)
@@ -41,7 +41,7 @@ Completed written guides can be found in the corresponding challenge directory (
4141
11. [x] ~~[Custom Video Player](./exercises/11\ -\ Custom\ Video\ Player/)~~
4242
12. [x] ~~[Key Sequence Detection](./exercises/12\ -\ Key\ Sequence\ Detection/)~~
4343
13. [x] ~~[Slide in on Scroll](./exercises/13\ -\ Slide\ in\ on\ Scroll/)~~
44-
14. [ ] JavaScript References vs. Copying
44+
14. [x] ~~[JavaScript References vs. Copying](./exercises/14\ -\ JavaScript\ References\ VS\ Copying)~~
4545
15. [ ] LocalStorage
4646
16. [ ] Mouse Move Shadow
4747
17. [ ] Sort Without Articles

0 commit comments

Comments
 (0)