/index.js

https://gitlab.com/zuitt-projects-asuncion/s18 · JavaScript · 186 lines · 90 code · 35 blank · 61 comment · 0 complexity · 2cc53251eddfea56c7df0de4083af532 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") //to invoke Laptop, use "new". It creates a new instance of an object
  39. console.log(laptop);
  40. let laptop1 = Laptop("Asus", 2010);
  41. console.log(laptop1); //result : undefined because the "new" word was not used to define it as an object
  42. // Creating empty objects
  43. let computer = {};
  44. let myComputer = new Object();
  45. console.log(computer);
  46. console.log(myComputer);
  47. // Accessing Array Objects
  48. let machines = [laptop, laptop1];
  49. //access the property of an object inside an array
  50. console.log(machines[0].name);
  51. //result : Lenovo
  52. //another way of accessing properties in an array
  53. console.log(machines[0]["releaseDate"]);
  54. // result: 2008
  55. // Initializing / Adding /Deleting /Reassigning Object Properties
  56. let car = {};
  57. // initializing or adding object properties using dot notation.
  58. car.name = "Honda Civic";
  59. console.log(car);
  60. // adding object properties with square brackets which is basically the same as using dot notation (car.name is the same as car["manufactureDate"])
  61. car["manufactureDate"] = 2019;
  62. console.log(car);
  63. // deleting object properties
  64. delete car["name"];
  65. console.log(car);
  66. /*
  67. Mini-Activity
  68. 1. Add properties to the object car:
  69. a. brand
  70. b. model
  71. c. color
  72. d. location
  73. */
  74. car.brand = "Honda"
  75. car.model = "Civic"
  76. car.color = "red"
  77. car.location = "La Union"
  78. console.log(car);
  79. // Reassigning object properties
  80. car.manufactureDate = 1986
  81. console.log(car);
  82. // Object Methods
  83. // a method is a function which is a property of an object
  84. // they are also functions and one of the key differences they have is that methods are functions realted to a specific object.
  85. //methods are defined based on what an object is capable of doing and how it should work.
  86. let person = {
  87. name: "Jin",
  88. //function talk()
  89. talk :function(){
  90. console.log(`Hello my name is ${this.name}`)
  91. }
  92. };
  93. person.talk();
  94. let friend = {
  95. firstName : "Bam",
  96. lastName : "Jeon",
  97. address : {
  98. city: "Caloocan",
  99. country: "Philippines"
  100. },
  101. emails : ["bam@mail.com", "jambam@gmail.com"],
  102. introduce : function(){
  103. console.log(`Hello my name is ${this.firstName} ${this.lastName}, I live in ${this.address.city}. My email is ${this.emails[0]}`)
  104. }
  105. };
  106. friend.introduce();
  107. // Real World Application
  108. // Scenario:
  109. // 1. Create a game that would have several pokemons to interact with each other.
  110. // 2. Every pokemon should have stats, properties, functions
  111. let myPokemon = {
  112. name : "Bulbasaur",
  113. level : 3,
  114. health : 100,
  115. attack : 50,
  116. tackle : function(){
  117. console.log(`${this.name} tackled another pokemon`)
  118. console.log(`targetPokemon's health is now reduced`)
  119. },
  120. faint: function(){
  121. console.log(`${this.name} fainted`)
  122. }
  123. };
  124. myPokemon.tackle();
  125. myPokemon.faint();
  126. // Creating object with an object constructor
  127. function Pokemon(name, lvl, hp){
  128. //Properties
  129. this.name = name;
  130. this.lvl = lvl;
  131. this.health = hp * 2;
  132. this.attack = lvl;
  133. // methods
  134. this.tackle = function(target){
  135. console.log(`${this.name} tackled ${target.name}`);
  136. console.log(`targetPokemon's health is now reduced`);
  137. console.log(target.name);
  138. console.log(this.name);
  139. };
  140. this.faint = function(){
  141. console.log(`${this.name} fainted`)
  142. }
  143. };
  144. // create new instances of the Pokemon object each with their unique properties
  145. let pikachu = new Pokemon("Pikachu", 3, 50);
  146. let ratata = new Pokemon("Ratata", 5, 25);
  147. // Providing the "ratata" object as an argument to "pikachu" tackle method will create interaction between the two objects.
  148. pikachu.tackle(ratata);
  149. ratata.tackle(pikachu);