/index.js

https://gitlab.com/zuitt-projects-bumaa/s18 · JavaScript · 189 lines · 91 code · 45 blank · 53 comment · 0 complexity · 0df29b2a74ec274233688ba2263f99d1 MD5 · raw file

  1. // alert("gm!!");
  2. // array - single variable with multiple values (no specificity)
  3. // object - contains multiple values (which are easily labeled)
  4. // -use properties instead of index
  5. // -values can have labels
  6. //JavaScript Objects
  7. /*
  8. Syntax:
  9. let objectName = {
  10. key : value
  11. key : (function)
  12. key : object {
  13. key : value
  14. }
  15. key : [array]
  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. // new operator creates an object
  37. let laptop = new Laptop("Lenovo", 2008);
  38. console.log(laptop);
  39. // will display new object
  40. //
  41. let laptop1 = Laptop("Asus", 2010);
  42. console.log(laptop1);
  43. //result: undefined
  44. // We can also create empty objects:
  45. let computer = {};
  46. let myComputer = new Object();
  47. console.log(myComputer);
  48. console.log(computer);
  49. // Accessing Array Objects ----- use a dot notation (.name) or ""
  50. // dot notation is better. "" may be confusing
  51. let machines = [laptop, laptop1];
  52. //access property of object inside an array
  53. console.log(machines[0].name)
  54. // result: Lenovo
  55. console.log(machines[0]["releaseDate"]);
  56. // Initializing, Adding, Deleting, Reassigning Object Properties
  57. let car = {};
  58. // initializing/adding object properties using dot notation
  59. car.name = "Honda Civic";
  60. console.log(car);
  61. // adding object properties using [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. car.brand = "Toyota",
  69. car.model = "Altis",
  70. car.color = "white",
  71. car.gasType = "Diesel",
  72. car.location = "Baguio City"
  73. console.log(car);
  74. // reassigning value of object properties
  75. car.manufactureDate = 1986;
  76. console.log(car);
  77. // Object Methods
  78. // - function which is a property of an object
  79. // - function that is related to the object
  80. // - methods are defined based on what an object is capable of doing & how it should work
  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},
  98. I live in ${this.address.city}`)
  99. }
  100. };
  101. friend.introduce();
  102. // real world application
  103. let myPokemon = {
  104. name: "Bulbasaur",
  105. level: 3,
  106. health: 100,
  107. attack: 50,
  108. tackle: function() {
  109. console.log(`${this.name} tackled another pokemon`)
  110. console.log(`targetPokemon's health is now reduced`)
  111. },
  112. faint: function() {
  113. console.log(`${this.name} fainted`)
  114. }
  115. };
  116. myPokemon.tackle();
  117. myPokemon.faint();
  118. // use object constructor
  119. function Pokemon(name, lvl, hp){
  120. //Properties
  121. this.name = name;
  122. this.lvl = lvl;
  123. this.health = hp * 2;
  124. this.attack = lvl;
  125. //methods
  126. this.tackle = function(target){
  127. console.log(`${this.name} tackled ${target.name}`);
  128. console.log(`targetPokemon's health is now reduced`);
  129. console.log(target.name);
  130. console.log(this.name);
  131. };
  132. this.faint = function() {
  133. console.log(`${this.name} fainted`);
  134. }
  135. };
  136. // create new instances of the Pokemon object each w/ their unique properties
  137. let pikachu = new Pokemon("Pikachu", 3, 50);
  138. let ratata = new Pokemon("Ratata", 5, 25);
  139. // Providing the "ratata" object as an argument to "pikachu" tackle method
  140. // will cteate interaction between two objects
  141. pikachu.tackle(ratata);
  142. ratata.tackle(pikachu);