/index.js

https://gitlab.com/zuitt-projects-gepayo/s18 · JavaScript · 203 lines · 102 code · 49 blank · 52 comment · 1 complexity · a0dfd55ba7c4bdc3659b85e252ab1283 MD5 · raw file

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