Guest User

Untitled

a guest
Jan 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. Let’s create a person in javascript. We could use a variable...
  2.  
  3. ```
  4. const person = "John Doe";
  5. ```
  6.  
  7. To print our person’s name we could do...
  8.  
  9. ```
  10. const person = "John Doe";
  11. console.log(person)
  12. ```
  13.  
  14. Well a person has more than a name so let’s add an age and eye color. We could use an array...
  15.  
  16. ```
  17. const person = ["John Doe", 50, "Blue"]
  18. ```
  19.  
  20. To print our person’s name, age, and eye color we could do...
  21.  
  22. ```
  23. const person = ["John Doe", 150, "Blue"]
  24.  
  25. console.log(person[0]) // John Doe
  26.  
  27. console.log(person[1]) // 150
  28.  
  29. console.log(person[2]) // Blue
  30. ```
  31.  
  32. Ok, now lets print a sentence with our person in it like we might do on a webpage, maybe a profile.
  33.  
  34. ```
  35. const person = ["John Doe", 150, "Blue"]
  36.  
  37. console.log("The great" + person[0] + ", with striking " + person[2] + " eyes, was a spry " + person[1] + " years old.");
  38. ```
  39.  
  40. There are other qualities we might want to add to describe our person like maybe species, number of legs, and number of arms. We could do…
  41.  
  42. ```
  43. const person = ["John Doe", 150, "Blue", "Human", 2, 2]
  44. ```
  45.  
  46. Ok, now lets print a sentence with our person in it like we might do on a webpage again.
  47.  
  48. ```
  49. const person = ["John Doe", 150, "Blue", "Human", 2, 2]
  50.  
  51. console.log("The great" + person[0] + ", with striking " + person[2] + " eyes, was a spry " + person[1] + " years old. A " + person[3] + "with " + person[4) + "legs and " + person[5] + " arms.");
  52. ```
  53.  
  54. Yikes, that doesn’t look very readable. It seems like we should use an object to organize our data better. Let’s create a person object and we might as well also separate the first and last name too.
  55.  
  56. ```
  57. const person = {
  58. species: "human",
  59. legs: 2,
  60. arms: 2,
  61. firstName: "John",
  62. lastName: "Doe",
  63. age: 150,
  64. eyeColor: "Blue"
  65. };
  66. ```
  67.  
  68. What if we want two different people. We could do
  69.  
  70. ```
  71. const person1 = {
  72. species: “human”,
  73. legs: 2,
  74. arms: 2,
  75. firstName: “John”,
  76. lastName: “Doe”,
  77. age: 150,
  78. eyeColor: “Blue”
  79. };
  80.  
  81. const person2 = {
  82. species: "human",
  83. legs: 2,
  84. arms: 2,
  85. firstName: "Jane",
  86. lastName: "Jackson",
  87. age: 200,
  88. eyeColor: "Green"
  89. };
  90. ```
  91.  
  92. Now when we write our sentence again, it is much easier to read!
  93.  
  94. ```
  95. const person1 = {
  96. species: "human",
  97. legs: 2,
  98. arms: 2,
  99. firstName: "John",
  100. lastName: "Doe",
  101. age: 150,
  102. eyeColor: "Blue"
  103. };
  104.  
  105. const person2 = {
  106. species: "human",
  107. legs: 2,
  108. arms: 2,
  109. firstName: "Jane",
  110. lastName: "Jackson",
  111. age: 200,
  112. eyeColor: "Green"
  113. };
  114.  
  115. console.log("The great" + person1.firstName + " " + person1.lastName + ", with striking " + person1.eyeColor + " eyes, was a spry " + person1.age + " years old. A " + person1.species + "with " + person1.legs + "legs and " + person1.arms + " arms.");
  116. ```
  117.  
  118. That’s ok. But if we wanted to create 10 or 20 people then we are going to be repeating a lot of code. Let’s build an object as a function ...
  119.  
  120. ```
  121. const Person = function(first, last, years, eye){
  122. this.firstName = first;
  123. this.lastName = last;
  124. this.age = years;
  125. this.eyeColor = eye;
  126. this.species = "Human";
  127. this.legs = 2;
  128. this.arms = 2
  129. }
  130. ```
  131.  
  132. Now our person will always start with the species of human, and have 2 legs and 2 arms but each person can be created with their own first name, last name, age, and eye color. We can create our two people again like this...
  133.  
  134. ```
  135. const Person = function(first, last, years, eye){
  136. this.firstName = first;
  137. this.lastName = last;
  138. this.age = years;
  139. this.eyeColor = eye;
  140. this.species = "Human";
  141. this.legs = 2;
  142. this.arms = 2
  143. };
  144.  
  145. const person1 = new Person("John", "Doe", 150, "Blue");
  146. const person2 = new Person("Jane", "Jackson", 200, "Green");
  147. ```
  148.  
  149. Now for 10-20 people we would only need to add 1 new line for each person! That’s going to save us lots of time!
  150. A quick note on common practices. We generally will name the parameters that the object accepts the same as the property. It can look confusing at first and it is important to know that they do not have to match which is why I wrote it like that originally. It is better practice, though, to name the parameter the same as the property to make sure there is no confusion about what the parameter is for. To write it like that we could do...
  151.  
  152. ```
  153. const Person = function(firstName, lastName, age, eyeColor){
  154. this.firstName = firstName;
  155. this.lastName = lastName;
  156. this.age = age;
  157. this.eyeColor = eyeColor;
  158. this.species = "Human";
  159. this.legs = 2;
  160. this.arms = 2
  161. };
  162.  
  163. const person1 = new Person("John", "Doe", 150, "Blue");
  164. const person2 = new Person("Jane", "Jackson", 200, "Green");
  165. ```
  166.  
  167. Let’s look at our sentence again.
  168.  
  169. ```
  170. const Person = function(firstName, lastName, age, eyeColor){
  171. this.firstName = firstName;
  172. this.lastName = lastName;
  173. this.age = age;
  174. this.eyeColor = eyeColor;
  175. this.species = "Human";
  176. this.legs = 2,
  177. this.arms = 2
  178. };
  179.  
  180. const person1 = new Person("John", "Doe", 150, "Blue");
  181. const person2 = new Person("Jane", "Jackson", 200, "Green");
  182.  
  183. console.log("The great" + person1.firstName + " " + person1.lastName + ", with striking " + person1.eyeColor + " eyes, was a spry " + person1.age + " years old. A " + person1.species + "with " + person1.legs + "legs and " + person1.arms + " arms.");
  184. ```
  185.  
  186. We still are only logging our sentence for person1. We could repeat the console.log and change person1 to person2 so that we can print a sentence for both like this
  187.  
  188. ```
  189. const Person = function(firstName, lastName, age, eyeColor){
  190. this.firstName = firstName;
  191. this.lastName = lastName;
  192. this.age = age;
  193. this.eyeColor = eyeColor;
  194. this.species = "Human";
  195. this.legs = 2;
  196. this.arms = 2
  197. };
  198.  
  199. const person1 = new Person("John", "Doe", 150, "Blue");
  200. const person2 = new Person("Jane", "Jackson", 200, "Green");
  201.  
  202. console.log("The great" + person1.firstName + " " + person1.lastName + ", with striking " + person1.eyeColor + " eyes, was a spry " + person1.age + " years old. A " + person1.species + "with " + person1.legs + "legs and " + person1.arms + " arms.");
  203. console.log("The great" + person2.firstName + " " + person2.lastName + ", with striking " + person2.eyeColor + " eyes, was a spry " + person2.age + " years old. A " + person2.species + "with " + person2.legs + "legs and " + person2.arms + " arms.");
  204. ```
  205.  
  206. Ok this is going to get really repetitive if we add 10-20 people again. We should put all of our people into an array and just loop through the array. In the loop, we can console.log the sentence for each person in the array. It would look like this.
  207.  
  208. ```
  209. const Person = function(firstName, lastName, age, eyeColor){
  210. this.firstName = firstName;
  211. this.lastName = lastName;
  212. this.age = age;
  213. this.eyeColor = eyeColor;
  214. this.species = "Human";
  215. this.legs = 2;
  216. this.arms = 2
  217. };
  218.  
  219. const person1 = new Person("John", "Doe", 150, "Blue");
  220. const person2 = new Person("Jane", "Jackson", 200, "Green");
  221.  
  222. const people = [person1, person2];
  223.  
  224. for (let i = 0; i < people.length; i++){
  225. console.log("The great" + people[i].firstName + " " + people[i].lastName + ", with striking " + people[i].eyeColor + " eyes, was a spry " + people[i].age + " years old. A " + people[i].species + "with " + people[i].legs + "legs and " + people[i].arms + " arms.");
  226. }
  227. ```
  228.  
  229. Now we could add 10-20 people and only have to add 1 line for each person like before. This is much easier code to manage!
Add Comment
Please, Sign In to add comment