Understanding How Objects are Passed

Modern JavaScript Using Objects
2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.55
List Price:  €119.77
You save:  €34.22
£74.16
List Price:  £103.84
You save:  £29.67
CA$137.94
List Price:  CA$193.12
You save:  CA$55.18
A$152.78
List Price:  A$213.90
You save:  A$61.12
S$128.67
List Price:  S$180.14
You save:  S$51.47
HK$780.19
List Price:  HK$1,092.31
You save:  HK$312.11
CHF 80.30
List Price:  CHF 112.42
You save:  CHF 32.12
NOK kr1,004.34
List Price:  NOK kr1,406.11
You save:  NOK kr401.77
DKK kr638.63
List Price:  DKK kr894.11
You save:  DKK kr255.47
NZ$170.14
List Price:  NZ$238.20
You save:  NZ$68.06
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,172.20
List Price:  ৳17,041.56
You save:  ৳4,869.36
₹8,825.07
List Price:  ₹12,355.46
You save:  ₹3,530.38
RM422.33
List Price:  RM591.28
You save:  RM168.95
₦152,828.71
List Price:  ₦213,966.31
You save:  ₦61,137.60
₨28,385.35
List Price:  ₨39,740.62
You save:  ₨11,355.27
฿3,218.97
List Price:  ฿4,506.69
You save:  ฿1,287.72
₺4,125.04
List Price:  ₺5,775.22
You save:  ₺1,650.18
B$544.72
List Price:  B$762.63
You save:  B$217.91
R1,768.44
List Price:  R2,475.88
You save:  R707.44
Лв167.48
List Price:  Лв234.49
You save:  Лв67
₩139,005.46
List Price:  ₩194,613.20
You save:  ₩55,607.74
₪333.96
List Price:  ₪467.56
You save:  ₪133.59
₱5,689.88
List Price:  ₱7,966.06
You save:  ₱2,276.18
¥14,815.95
List Price:  ¥20,742.93
You save:  ¥5,926.97
MX$1,867.49
List Price:  MX$2,614.56
You save:  MX$747.07
QR364.04
List Price:  QR509.67
You save:  QR145.63
P1,439.16
List Price:  P2,014.89
You save:  P575.72
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,854.53
List Price:  E£6,796.54
You save:  E£1,942
ብር14,306.63
List Price:  ብር20,029.86
You save:  ብር5,723.22
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$97,214.27
List Price:  CLP$136,103.87
You save:  CLP$38,889.60
CN¥713
List Price:  CN¥998.24
You save:  CN¥285.23
RD$6,340.36
List Price:  RD$8,876.76
You save:  RD$2,536.40
DA12,985.04
List Price:  DA18,179.58
You save:  DA5,194.53
FJ$225.61
List Price:  FJ$315.87
You save:  FJ$90.25
Q767.35
List Price:  Q1,074.33
You save:  Q306.97
GY$20,923.93
List Price:  GY$29,294.34
You save:  GY$8,370.41
ISK kr12,250.77
List Price:  ISK kr17,151.57
You save:  ISK kr4,900.80
DH907.55
List Price:  DH1,270.61
You save:  DH363.06
L1,676.34
List Price:  L2,346.95
You save:  L670.60
ден5,281.40
List Price:  ден7,394.17
You save:  ден2,112.77
MOP$803.54
List Price:  MOP$1,124.98
You save:  MOP$321.44
N$1,773.82
List Price:  N$2,483.42
You save:  N$709.60
C$3,680.31
List Price:  C$5,152.59
You save:  C$1,472.27
रु14,096.01
List Price:  रु19,734.98
You save:  रु5,638.96
S/352.21
List Price:  S/493.11
You save:  S/140.90
K423.82
List Price:  K593.36
You save:  K169.54
SAR375.17
List Price:  SAR525.26
You save:  SAR150.08
ZK2,380.02
List Price:  ZK3,332.12
You save:  ZK952.10
L434.52
List Price:  L608.35
You save:  L173.82
Kč2,090.29
List Price:  Kč2,926.49
You save:  Kč836.19
Ft33,616.37
List Price:  Ft47,064.26
You save:  Ft13,447.89
SEK kr940.94
List Price:  SEK kr1,317.36
You save:  SEK kr376.41
ARS$136,236.96
List Price:  ARS$190,737.20
You save:  ARS$54,500.23
Bs691.36
List Price:  Bs967.94
You save:  Bs276.57
COP$399,414.69
List Price:  COP$559,196.55
You save:  COP$159,781.85
₡50,561.46
List Price:  ₡70,788.07
You save:  ₡20,226.60
L2,620.33
List Price:  L3,668.57
You save:  L1,048.24
₲720,925.20
List Price:  ₲1,009,324.12
You save:  ₲288,398.92
$U4,006.62
List Price:  $U5,609.43
You save:  $U1,602.81
zł363.92
List Price:  zł509.51
You save:  zł145.58
Already have an account? Log In

Transcript

What do I mean by objects are passed by reference and not copied? Well, this is an important concept to understand. If you are creating, quote unquote copies of objects, the best way to explain it is to show an example. So let me open up the console. Once again, I'm going to create our object. I have a number of different properties in that object and a method.

Now let's say I wanted a copy of this object. So I could do something like this. User one equals user. Now if I display user, it looks like this. We can open that up and see everything in that object. If I display user one.

It looks like this. looks exactly the same. And that might lead you to think, well now I have two copies. Well, that is not completely true. Basically, these variables are simply references to an object, this object is stored in memory somewhere. And these variables reference that.

And that's important to remember because if you change something about one, it will change it about the other because they point to the same object. So for example, let's write now the age is set to 32. So let me change user dot age and set that equal to 33. Now Now if I display user, we can see age is 33. How about if we display user one ages also 33 so it changed it for both. And the reason it does that is once again, we did not create a copy, we don't have two separate objects, we have a single object stored in memory.

And then these variables are referencing that same object. So very important concept to remember if you ever create more than one reference to an object, simply remember, it is not a copy of that object. If you wanted a separate copy, then we would have to execute our original command where we created it, assign it to a different variable. Now user two is of course its own object. Alright, let's move on to the next section.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.