/index.js

https://gitlab.com/zuitt-projects-rondina/s18 · JavaScript · 197 lines · 88 code · 47 blank · 62 comment · 0 complexity · 168c930c742420ab6862dac48768a501 MD5 · raw file

  1. /*alert("HELLO");*/
  2. // A VARIABLE WITH MULTIPLE VALUES
  3. // a : array
  4. //
  5. // JS OBJECT
  6. // like array , it contains multiple values
  7. // unlike an array
  8. // STRUCTURE
  9. /*
  10. let objectName = {
  11. key : value;
  12. key : (function)
  13. key : object {
  14. key : value
  15. }
  16. }
  17. */
  18. let cellphone = {
  19. name : "Nokia 3210",
  20. manufactureDate : 1999
  21. };
  22. console.log(cellphone);
  23. console.log(typeof cellphone);
  24. //CREATING OBJECTS USING A CONSTRUCTOR FUNCTION
  25. /*
  26. SYNTAX :
  27. function ObjectName(keyA, keyB){
  28. this.keyA = keyA;
  29. this.keyB = keyB;
  30. }
  31. */
  32. function Laptop(name, manufactureDate) {
  33. this.name = name;
  34. this.releaseDate = manufactureDate
  35. };
  36. let laptop = new Laptop("Lenovo", 2008);
  37. console.log(laptop);
  38. // will create new object
  39. // new operator creates an instance of an object
  40. let laptop1 = Laptop("Asus", 2010);
  41. console.log(laptop1);
  42. // results : undefined
  43. // CREATING EMPTY OBJECTS
  44. let computer = {};
  45. let myComputer = new Object();
  46. console.log(computer);
  47. console.log(myComputer);
  48. // ACCESSING ARRAY OBJECTS
  49. let machines = [laptop, laptop1];
  50. // access the property of an object inside an array
  51. console.log(machines[0].name);
  52. // result : Lenovo
  53. //another way of accessing properties in an array
  54. console.log(machines[0]["releaseDate"]);
  55. // result : 2008
  56. // INITIALIZING / ADDING / DELETING / REASSIGNING OBJECT PROPERTIES
  57. let car = {};
  58. // initializing/adding object properties using dot nototation
  59. car.name = "Honda Civic";
  60. console.log(car);
  61. //another way of adding object property with square brackets
  62. car["manufactureDate"] = 2019;
  63. console.log(car);
  64. //deleting object properties
  65. delete car["name"];
  66. console.log(car);
  67. /*MINI ACTIVITY
  68. add properties to the object car */
  69. car.brand = "Nissan";
  70. car.model = "Almera";
  71. car.color = "black";
  72. car.location = "Cagayan De Oro";
  73. console.log(car);
  74. //Reassigning object properties
  75. car.manufactureDate = 2016;
  76. // OBJECT METHODS
  77. // a method is a function which is a property of an object
  78. //they are also functions and one of the key differences they have is that methods
  79. // are functions related to a specific object
  80. // methods are defined based on what an object is capable of doing and how it should works
  81. let person = {
  82. name : "Jin",
  83. talk : function(){
  84. console.log(`Hello my name is ${this.name}`)
  85. }
  86. };
  87. person.talk();
  88. let friend = {
  89. firstName : "Bam",
  90. lastName : "Jeon",
  91. address : {
  92. city : "Caloocan",
  93. country : "Philippines"
  94. },
  95. emails : ["bam@mail.com", "jeonbam@gmail.com"],
  96. introduce : function(){
  97. console.log(`Hello my name is ${this.firstName} ${this.lastName},I live in ${this.address.city}. My email is ${this.emails[0]}`)
  98. }
  99. };
  100. friend.introduce();
  101. // REAL WORLD APPLICATION
  102. // SCENARIO:
  103. // 1. Create a game that would have several pokemns to interact with each other
  104. // 2. Every pokemen should have stats, properties, functions
  105. let myPokemen = {
  106. name : "Bulbasaur",
  107. level : 3,
  108. health : 100,
  109. attack : 50,
  110. tackle : function(){
  111. console.log(`${this.name} tackled another pokemon`)
  112. console.log(`targetPokemen's health is now reduced`)
  113. },
  114. faint: function (){
  115. console.log(`${this.name} fainted`)
  116. }
  117. };
  118. myPokemen.tackle();
  119. myPokemen.faint();
  120. // Creating object with an object constructor
  121. function Pokemon(name, lvl, hp){
  122. //Properties
  123. this.name = name;
  124. this.lvl = lvl;
  125. this.health = hp * 2 ;
  126. this.attack = lvl;
  127. // methods
  128. this.tackle = function(target){
  129. console.log(`${this.name} tackled ${target.name}`);
  130. console.log(`targetPokemen's health is now reduced`);
  131. console.log(target.name);
  132. console.log(this.name);
  133. };
  134. this.faint = function(){
  135. console.log(`${this.name} fainted`)
  136. }
  137. };
  138. // create new instances of the Pokemo object each with their unique properties
  139. let pikachu = new Pokemon("Pikachu", 3, 50);
  140. let ratata = new Pokemon("Ratata", 5, 25);
  141. // Providing the "ratata" object as an argument to "pikachu" tackle method
  142. //will creatE interaction between the two objects:
  143. pikachu.tackle(ratata);
  144. ratata.tackle(pikachu);