/index.js

https://gitlab.com/zuitt-projects-pableo/s18 · JavaScript · 183 lines · 88 code · 38 blank · 57 comment · 0 complexity · 9b889631b254ffafb47ba7ef14aed7ad MD5 · raw file

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