|
| 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 | + |
0 commit comments