/index.js

https://gitlab.com/zuitt-projects-loresto/s18 · JavaScript · 174 lines · 90 code · 31 blank · 53 comment · 0 complexity · 2e8ab11713244f529ac4c46e777a2805 MD5 · raw file

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