/index.js

https://gitlab.com/zuitt-projects-reyes/s18 · JavaScript · 193 lines · 90 code · 39 blank · 64 comment · 0 complexity · e2b9f730af2dea2756b73ddfc0f83927 MD5 · raw file

  1. // alert("hi");
  2. // A variable with multiple values
  3. //a: array
  4. let cartoons = ["Elmo", "Mickey", "Daisy", "Barbie"]
  5. // Javascript Objects
  6. // similar to an array, it also contains multiple values
  7. // unlike an array that uses index, objects use properties
  8. // with object, we can easily give labels to each value
  9. // Structure or Syntax
  10. /*
  11. let objectName = {
  12. key : value,
  13. key : (function),
  14. key : object{
  15. key : value
  16. },
  17. key : [array]
  18. }
  19. */
  20. let cellphone = {
  21. name: "Nokia 3210",
  22. manufactureDate: 1999
  23. };
  24. console.log(cellphone);
  25. console.log(typeof cellphone);
  26. // Creating objects using a constructor function
  27. /*
  28. Syntax:
  29. function objectName(keyA, keyB){
  30. this.keyA = keyA;
  31. this.keyB = keyB;
  32. }
  33. */
  34. function Laptop(name, manufactureDate){
  35. this.name = name;
  36. this.releaseDate = manufactureDate;
  37. };
  38. let laptop = new Laptop("Lenovo", 2008);
  39. console.log(laptop);
  40. // will create new object
  41. // new operator creates an instance of an object
  42. let laptop1 = Laptop("Asus", 2010);
  43. console.log(laptop1);
  44. // result: undefined
  45. // Creating empty objects
  46. let computer = {};
  47. let myComputer = new Object();
  48. console.log(computer);
  49. console.log(myComputer);
  50. // Accessing Array Objects
  51. let machines = [laptop, laptop1];
  52. // access the property of an object inside an array
  53. console.log(machines[0].name);
  54. // result: Lenovo
  55. // another way of accessing properties in an array
  56. console.log(machines[0]["releaseDate"]);
  57. // result: 2008
  58. // Initializing/ Adding/ Deleting/ Reassigning Object Properties
  59. let car = {};
  60. // initializing/ adding object properties using dot notation
  61. car.name = "Honda Civic";
  62. console.log(car);
  63. // adding object properties with square brackets
  64. car["manufactureDate"] = 2019;
  65. console.log(car);
  66. // deleting object properties
  67. delete car["name"];
  68. console.log(car);
  69. /*
  70. Mini-Activity
  71. 1. Add properties to the object car:
  72. a. brand
  73. b. model
  74. c. color
  75. e. location
  76. 2. Print in your browser console the car object and send your screenshot in our batch hangouts
  77. */
  78. car.brand = "Jeep"
  79. car.model = "Wrangler"
  80. car.color = "black"
  81. car.location = "Metro Manila"
  82. console.log(car);
  83. // Reassigning object properties
  84. car.manufactureDate = 1986
  85. console.log(car);
  86. // Object Methods
  87. // a method is a function which is a property of an object
  88. // they are also functions and one of the key differences they have is that methods are functions related to a specific object
  89. // methods are defined based on what an object is capable of doing and how it should work
  90. let person = {
  91. name: "Jin",
  92. talk: function(){
  93. console.log(`Hello my name is ${this.name}`)
  94. }
  95. };
  96. person.talk();
  97. let friend = {
  98. firstName: "Bam",
  99. lastName: "Jeon",
  100. address: {
  101. city: "Caloocan",
  102. country: "Philippines"
  103. },
  104. emails: ["bam@mail.com", "jeonbam@gmail.com"],
  105. introduce: function(){
  106. console.log(`Hello my name is ${this.firstName} ${this.lastName}, I live in ${this.address.city}. My personal email is ${this.emails[0]}`)
  107. }
  108. };
  109. friend.introduce();
  110. // Real World Application
  111. // Scenario:
  112. // 1. Create a game that would have several pokemons to interact with each other
  113. // 2. Every pokemon should have stats, properties, functions
  114. let myPokemon = {
  115. name: "Bulbasaur",
  116. level: 3,
  117. health: 100,
  118. attack: 50,
  119. tackle: function(){
  120. console.log(`${this.name} tackled another pokemon`)
  121. console.log(`targetPokemon's health is now reduced`)
  122. },
  123. faint: function(){
  124. console.log(`${this.name} fainted`)
  125. }
  126. };
  127. myPokemon.tackle();
  128. myPokemon.faint();
  129. // Creating object with an object constructor
  130. function Pokemon(name, lvl, hp){
  131. // Properties
  132. this.name = name;
  133. this.lvl = lvl;
  134. this.health = hp * 2;
  135. this.attack = lvl;
  136. // methods
  137. this.tackle = function(target){
  138. console.log(`${this.name} tackled ${target.name}`);
  139. console.log(`targetPokemon's health is now reduced`);
  140. console.log(target.name);
  141. console.log(this.name);
  142. };
  143. this.faint = function(){
  144. console.log(`${this.name} fainted`)
  145. }
  146. };
  147. // create new instances of the Pokemon object each with their unique properties
  148. let pikachu = new Pokemon("Pikachu", 3, 50);
  149. let ratata = new Pokemon("Ratata", 5, 25);
  150. // Providing the "ratata" object as an argument to "pikachu" tackle method will create interaction between the two objects
  151. pikachu.tackle(ratata);
  152. ratata.tackle(pikachu);