How to Create Objects - 2

5 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$39.99
List Price:  $79.99
You save:  $40
€34.21
List Price:  €68.43
You save:  €34.22
£29.66
List Price:  £59.33
You save:  £29.67
CA$55.16
List Price:  CA$110.35
You save:  CA$55.18
A$61.10
List Price:  A$122.22
You save:  A$61.12
S$51.46
List Price:  S$102.93
You save:  S$51.47
HK$312.03
List Price:  HK$624.14
You save:  HK$312.11
CHF 32.11
List Price:  CHF 64.24
You save:  CHF 32.12
NOK kr401.67
List Price:  NOK kr803.45
You save:  NOK kr401.77
DKK kr255.41
List Price:  DKK kr510.89
You save:  DKK kr255.47
NZ$68.04
List Price:  NZ$136.11
You save:  NZ$68.06
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,868.14
List Price:  ৳9,737.51
You save:  ৳4,869.36
₹3,529.50
List Price:  ₹7,059.88
You save:  ₹3,530.38
RM168.90
List Price:  RM337.86
You save:  RM168.95
₦61,122.31
List Price:  ₦122,259.91
You save:  ₦61,137.60
₨11,352.43
List Price:  ₨22,707.71
You save:  ₨11,355.27
฿1,287.39
List Price:  ฿2,575.11
You save:  ฿1,287.72
₺1,649.77
List Price:  ₺3,299.95
You save:  ₺1,650.18
B$217.85
List Price:  B$435.76
You save:  B$217.91
R707.27
List Price:  R1,414.71
You save:  R707.44
Лв66.98
List Price:  Лв133.98
You save:  Лв67
₩55,593.84
List Price:  ₩111,201.58
You save:  ₩55,607.74
₪133.56
List Price:  ₪267.16
You save:  ₪133.59
₱2,275.61
List Price:  ₱4,551.79
You save:  ₱2,276.18
¥5,925.49
List Price:  ¥11,852.46
You save:  ¥5,926.97
MX$746.88
List Price:  MX$1,493.95
You save:  MX$747.07
QR145.59
List Price:  QR291.22
You save:  QR145.63
P575.58
List Price:  P1,151.30
You save:  P575.72
KSh5,166.70
List Price:  KSh10,334.70
You save:  KSh5,168
E£1,941.52
List Price:  E£3,883.53
You save:  E£1,942
ብር5,721.79
List Price:  ብር11,445.02
You save:  ብር5,723.22
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,879.87
List Price:  CLP$77,769.47
You save:  CLP$38,889.60
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,535.76
List Price:  RD$5,072.16
You save:  RD$2,536.40
DA5,193.23
List Price:  DA10,387.77
You save:  DA5,194.53
FJ$90.23
List Price:  FJ$180.48
You save:  FJ$90.25
Q306.89
List Price:  Q613.87
You save:  Q306.97
GY$8,368.31
List Price:  GY$16,738.73
You save:  GY$8,370.41
ISK kr4,899.57
List Price:  ISK kr9,800.37
You save:  ISK kr4,900.80
DH362.96
List Price:  DH726.02
You save:  DH363.06
L670.43
List Price:  L1,341.04
You save:  L670.60
ден2,112.24
List Price:  ден4,225.01
You save:  ден2,112.77
MOP$321.36
List Price:  MOP$642.81
You save:  MOP$321.44
N$709.42
List Price:  N$1,419.02
You save:  N$709.60
C$1,471.90
List Price:  C$2,944.18
You save:  C$1,472.27
रु5,637.55
List Price:  रु11,276.52
You save:  रु5,638.96
S/140.86
List Price:  S/281.76
You save:  S/140.90
K169.50
List Price:  K339.04
You save:  K169.54
SAR150.04
List Price:  SAR300.13
You save:  SAR150.08
ZK951.86
List Price:  ZK1,903.96
You save:  ZK952.10
L173.78
List Price:  L347.61
You save:  L173.82
Kč835.99
List Price:  Kč1,672.19
You save:  Kč836.19
Ft13,444.53
List Price:  Ft26,892.42
You save:  Ft13,447.89
SEK kr376.32
List Price:  SEK kr752.74
You save:  SEK kr376.41
ARS$54,486.61
List Price:  ARS$108,986.84
You save:  ARS$54,500.23
Bs276.50
List Price:  Bs553.08
You save:  Bs276.57
COP$159,741.91
List Price:  COP$319,523.76
You save:  COP$159,781.85
₡20,221.55
List Price:  ₡40,448.16
You save:  ₡20,226.60
L1,047.97
List Price:  L2,096.21
You save:  L1,048.24
₲288,326.82
List Price:  ₲576,725.74
You save:  ₲288,398.92
$U1,602.41
List Price:  $U3,205.22
You save:  $U1,602.81
zł145.54
List Price:  zł291.13
You save:  zł145.58
Already have an account? Log In

Transcript

The fifth and last way of creating objects in JavaScript is using the return in a function, you just return an object. So you just create a function and just add the return keyword. And then you create curly braces indicating an object, and you add your properties that you want to return. And it is going, it's going to return this object once he call this function. So let's get an example. And by the way, this for this function is also known as a factory function.

So let's get an example. So you just create a function for the factory, or method, or a factory or a factory function, you just create a function and call it person. And then you add the values that you want to pass name, as well as age, and then curly braces and inside, you just create an object a simple object. For example, if I create var person equals two curly braces, and then you want the name, you want to return the name, name equals the name that you pass, which is this one, and then the age, and then the age that you pass. But the thing is that this one, this variable is redundant. We don't, we don't have to here to create war person, it's redundant, you just can, instead of just typing your return, and then person, it's fine.

To do that you just create here, the object by using var person equals two curly braces and then the values and then you return that object. But this is not commonly used in JavaScript in JavaScript, what you would do instead, in order to make up your code shorter, you just delete this. It's redundant. As I told you, as well as this equal, and you just return, return this object, this object complete object. And since we pass these parameters, we don't even have to explicitly add the key and value pair, what we can do is just type name, and just type age. And JavaScript will immediately know that these values the name as well as the age are equal to the values that you pass.

But if the value does not exist as a parameter, you can just you can add it here expensively by using the key value pair. So here, I can just type. If I want another property, for example, a height, I would just say pi. And then the value which could be our default value, one, Aries centimeter so this is the fifth the fifth method. In order to create an object of this function, you just type, you just type, you just call the function without anything, just call the function person. And then you pass your parameters.

As a normal function, I'm going to pass key, as well as 25. And I'm going to store the result the return of this function into a variable called person one equals to this. And as you can see, what what this function what this function is going to return, it's going to return this object, which means that person one now equals to this object to this object. And of course, we can print it out using the console dot log, and then we pass person one, and semicolon if I save and run, I get this object with these three properties and we can also create as many variations as we want from this object. So I can here create another person called person, person two. And it's going to be equal to again, person.

But remember that this is a call to this function. So person, and they can pass whatever you want. So here I'm going to pass john and the age 23. And they can of course, again printed using console dot lock, and I pass person to and I Iran again. As you can see, I get another object in our inside person too. So this fifth method, which is called factory function is super powerful because you can create as many variations as you want from the same function.

So you just create the function one time and you create as many objects as you want.

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.