/index.js

https://gitlab.com/zuitt-projects-gonzalgo/s18 · JavaScript · 152 lines · 87 code · 21 blank · 44 comment · 0 complexity · 8c1f58788263bab954637467472fd39b MD5 · raw file

  1. //javascript objects
  2. //unlike array that uses index, objects use properties
  3. /*
  4. syntax:
  5. let objectName = {
  6. key: value,
  7. key: (function),
  8. key: object {
  9. key: value
  10. },
  11. key: [array]
  12. }
  13. */
  14. let cellphone = {
  15. name: "Nokia 3210",
  16. manufacturedDate: 1999
  17. };
  18. console.log(cellphone);
  19. console.log(typeof cellphone);
  20. //creating objects using constructor function
  21. /*
  22. syntax:
  23. function objectName(keyA, keyB) {
  24. this.keyA = keyA;
  25. this.keyB = keyB;
  26. };
  27. */
  28. function Laptop(name, manufacturedDate) {
  29. this.name = name;
  30. this.releasedDate = manufacturedDate;
  31. }
  32. //new operator creates an instance of an object
  33. console.log(new Laptop("Lenovo", 2008));
  34. console.log(Laptop("Asus", 2010));
  35. //result: undefined
  36. //creating empty objects
  37. let computer = {};
  38. console.log(computer);
  39. let myComputer = new Object();
  40. console.log(myComputer);
  41. //accessing array objects
  42. let machines = [cellphone];
  43. //access the property of an object inside an array
  44. console.log(machines[0].name);
  45. console.log(machines[0]["name"]);
  46. //initializing/ adding/ deleting/ reassigning object properties
  47. let car = {};
  48. //initialing/adding obeject properties using a dot (.) and [] notation
  49. car.name = "Honda Civic";
  50. console.log(car);
  51. car["manufacturedDate"] = 2019;
  52. console.log(car);
  53. //deleting object propeties
  54. delete car["name"];
  55. console.log(car);
  56. //mini-activity
  57. car.brand = "Toyota";
  58. car.model = "Avalon";
  59. car.color = "Red";
  60. car.location = "Cubao";
  61. console.log(car);
  62. //reassigning object properties
  63. car.manufacturedDate = 2022;
  64. console.log(car);
  65. //object methods
  66. //a method is a function which is a property of an object
  67. //they are also functions and one of the key differences they have is that methods are functions related to a specific object
  68. //methods are defined based on what an object is capable of doing and how it should work
  69. let person = {
  70. name: "Jin",
  71. talk: function() {
  72. console.log(`Hello my name is ${this.name}`);
  73. },
  74. }
  75. person.talk();
  76. let friend = {
  77. firstName: "Lisa",
  78. lastName: "Manobal",
  79. address: {
  80. country: "South Korea",
  81. city: "Seoul"
  82. },
  83. emails: ["lisa@mail.com", "lm@mail.com"],
  84. indroduce: function() {
  85. console.log(`Hello my name is ${this.firstName} ${this.lastName}, I live in ${this.address.city}. My personal email is ${this.emails[0]}`);
  86. },
  87. };
  88. friend.indroduce();
  89. //real world application
  90. //scenario:
  91. //1. create a game that would have several pokemons to interact with each other
  92. //2. every pokemon should have, stats, properties, functions
  93. let myPokemon = {
  94. name: "Bulbasaur",
  95. level: 3,
  96. health: 100,
  97. attack: 50,
  98. tackle: function() {
  99. console.log(`${this.name} takled another pokemon`);
  100. console.log(`targetPokemon's health is now reduced`);
  101. },
  102. faint: function() {
  103. console.log(`${this.name} fainted`);
  104. }
  105. };
  106. myPokemon.tackle();
  107. myPokemon.faint();
  108. //creating obeject with an object constructor
  109. function Pokemon(name, lvl, hp) {
  110. //properties
  111. this.name = name;
  112. this.lvl = lvl;
  113. this.health = hp * 2;
  114. this.attack = lvl;
  115. //methods
  116. this.tackle = function(target) {
  117. console.log(`${this.name} tackled ${target.name}`);
  118. console.log(`targetPokemon's health is now reduced`);
  119. console.log(target.name);
  120. console.log(this.name);
  121. }
  122. this.faint = function() {
  123. console.log(`${this.name} fainted`);
  124. }
  125. };
  126. //creat new instances of the pokemon object eahc with their unique properties
  127. let pikachu = new Pokemon("Pikachu", 3, 50);
  128. let ratata = new Pokemon("Ratata", 5, 25);
  129. //providing the "ratata" object as an argument to "pikachu" tackle method will create interaction
  130. pikachu.tackle(ratata);
  131. ratata.tackle(pikachu);