/index.js

https://gitlab.com/zuitt-projects-chua/s18 · JavaScript · 171 lines · 90 code · 39 blank · 42 comment · 0 complexity · 9dd57534d53290013d285389039bd8c3 MD5 · raw file

  1. /*alert("Hello")*/
  2. /*
  3. Javascript objects
  4. similar to an array containing multiple values
  5. properties:value
  6. let objectName = {
  7. key : value,
  8. key : (function),
  9. }
  10. */
  11. let cellphone = {
  12. name: "Nokia 3210",
  13. manufactureDate: 1999
  14. };
  15. console.log(cellphone);
  16. console.log(typeof cellphone);
  17. /*
  18. function objectName (keya, keyb){
  19. this.keya = keya;
  20. this.keyb = keyb;
  21. }
  22. */
  23. //object constructor
  24. function Laptop(name, manufactureDate){
  25. this.name = name;
  26. this.releaseDate = manufactureDate;
  27. /*console.log(`The laptop name is ${name} and manufactureDate at ${manufactureDate}`);*/
  28. //there is no return so the purpose is to create an object
  29. }
  30. let laptop = new Laptop("Lenovo", 2008);
  31. console.log(laptop);
  32. //new keyword used to create new objects (new container for function)
  33. let laptop1 = new Laptop("Asus", 2010);
  34. console.log(laptop1);
  35. let computer = {};
  36. //Laptop("huawei", 2019);
  37. let machines = [laptop, laptop1];
  38. console.log(machines[0].name);
  39. console.log(machines[1].name);
  40. console.log(machines[0]["releaseDate"]);
  41. console.log(machines[0].releaseDate);
  42. /*
  43. Initializing/Adding/Deleting/Reassigning Object Properties
  44. */
  45. let car = {};
  46. car.name = "Honda";
  47. console.log(car);
  48. car["manufactureDate"] = 2019;
  49. console.log(car);
  50. //delete object properties
  51. delete car["name"];
  52. console.log(car);
  53. //car.name; dont work
  54. car.brand = "Tesla";
  55. car.model = "Type A";
  56. car.color = "Bluish Gray";
  57. car.location = "USA";
  58. console.log(car);
  59. console.log(car.name);//undefined doesnt exist
  60. //Reassigning properties
  61. car.manufactureDate = 2021;
  62. console.log(car);
  63. /*
  64. Object methods
  65. method is a function
  66. methods are defined based on what an object is capable of doing and how it should work
  67. */
  68. let person = {
  69. name: "Jin",
  70. //talk is the function name as property
  71. talk: function(){
  72. console.log(`Hello my name is ${this.name}`);
  73. }
  74. };
  75. person.talk();
  76. console.log(person.name);
  77. //undefined not working console.log(person.talk());
  78. let friend ={
  79. firstName: "Bam",
  80. lastName: "Jeon",
  81. address: {
  82. city: "Caloocan",
  83. country: "Philippines"
  84. },
  85. emails: ["bam@mail.com", "jeonbam@gmail.com"],
  86. introduce: function(){
  87. console.log(`Hello my name is ${this.firstName} ${this.lastName} I live in ${this.address.city}. My personal email is ${this.emails[0]}`)
  88. }
  89. };
  90. //you can not call array, objects, without this/ properties or index included
  91. friend.introduce();
  92. let myPokemon = {
  93. name: "Bulbasur",
  94. level: 3,
  95. health: 100,
  96. attack: 50,
  97. tackle: function(){
  98. console.log(`${this.name} tackled another pokemon`)
  99. console.log(`target pokemon's health reduced by ${this.attack} points`)
  100. },
  101. faint: function(){
  102. console.log(`${this.name} fainted`)
  103. }
  104. }
  105. myPokemon.tackle();
  106. myPokemon.faint();
  107. /*
  108. Creating object with an object constructor
  109. */
  110. function Pokemon(name, lvl, hp){
  111. //properties
  112. this.name = name;
  113. this.lvl = lvl;
  114. this.health = hp * 2;
  115. this.attack = lvl;
  116. //methods
  117. this.tackle = function(target){
  118. console.log(`${this.name} tackled ${this.target.name}`)
  119. console.log(`Target pokemon health is now reduced`)
  120. console.log(target.name);
  121. console.log(this.name);
  122. this.faint = function(){
  123. console.log(`${this.: value,} fainted`)
  124. }
  125. }
  126. };
  127. let pikachu = new Pokemon("Pikachu")
  128. let ratata = new Pokemon("Ratata")
  129. pikachu.tackle(ratata);
  130. ratata.tackle(pikachu);