/index.js

https://gitlab.com/zuitt-projects-ayers/s18 · JavaScript · 179 lines · 90 code · 34 blank · 55 comment · 0 complexity · 545be5030f35a90a6afe2b4da215cb40 MD5 · raw file

  1. // A variable with mulitple values
  2. // a: array
  3. let cartoons = ["Elmo", "Mickey", "Daisy", "Barbie"]
  4. // JavaScript Objects
  5. // Similar to an array, it also contains multiple values.
  6. // Unlike an array that uses indexes, objects use properties.
  7. // With objects, we can easily give labels to each value.
  8. // Structure or Syntax
  9. /*
  10. let objectName = {
  11. key : value,
  12. key : (function),
  13. key : object{
  14. key : value
  15. },
  16. key : [array]
  17. }
  18. */
  19. let cellphone = {
  20. name : "Nokia 3210",
  21. manufactureDate : 1999
  22. };
  23. console.log(cellphone);
  24. console.log(typeof cellphone);
  25. // Creating Objects Using a Constructor Function
  26. /*
  27. Syntax:
  28. function objectName(keyA, keyB){
  29. this.keyA = keyA;
  30. this.keyB = keyB;
  31. };
  32. */
  33. function Laptop(name, manufactureDate){
  34. this.name = name;
  35. this.releaseDate = manufactureDate;
  36. };
  37. let laptop = new Laptop("Lenovo", 2008);
  38. console.log(laptop);
  39. // will create new object.
  40. // The new operator creates an instance of an object.
  41. let laptop1 = Laptop("Asus", 2010);
  42. console.log(laptop1);
  43. // result: undefined
  44. // (Lack of new operator creates undefined.)
  45. // Creating Empty Objects
  46. let computer = {};
  47. let myComputer = new Object();
  48. console.log(computer);
  49. console.log(myComputer);
  50. // Accessing Array Objects
  51. let machines = [laptop, laptop1];
  52. // One way to access the property of an object inside an array:
  53. console.log(machines[0].name);
  54. // result: Lenovo
  55. // Another way of accessing properties in an array.
  56. console.log(machines[0]["releaseDate"]);
  57. // result: 2008
  58. // Initializing/Adding/Deleting/Reassigning Object Properties
  59. let car = {};
  60. // initializing/adding object properties using dot notation
  61. car.name = "Honda Civic";
  62. console.log(car);
  63. // adding object properties with square brackets
  64. car["manufactureDate"] = 2019;
  65. console.log(car);
  66. // deleting object properties
  67. delete car["name"];
  68. console.log(car);
  69. car.brand = "Honda";
  70. car.model = "Civic";
  71. car.color = "black";
  72. car.location = "PH";
  73. console.log(car);
  74. // Reassigning object properties
  75. car.manufactureDate = 1986
  76. console.log(car);
  77. // Object Methods
  78. // A method is a function which is a property of an object
  79. // They are also functions, and one of the key differences they have is that methods are functions related to a specific object.
  80. // Methdos are defined based on what an object is capable of doing and how it should work.
  81. // (When making functions in an object, the key becomes the name of the function.)
  82. let person = {
  83. name : "Jin",
  84. talk : function(){
  85. console.log(`Hello, my name is ${this.name}.`);
  86. }
  87. };
  88. person.talk();
  89. let friend = {
  90. firstName : "Bam",
  91. lastName : "Jeon",
  92. address : {
  93. city : "Caloocan",
  94. country : "Philippines"
  95. },
  96. emails : ["bam@mail.com","jeonbam@mail.com"],
  97. introduce : function(){
  98. console.log(`Hello, my name is ${this.firstName} ${this.lastName}. I live in ${this.address.city}. My email is ${this.emails[0]}.`)
  99. }
  100. };
  101. friend.introduce();
  102. // Real World Application
  103. // Scenario:
  104. // 1. Create a game that would have several pokemons to interact with each other.
  105. // 2. Every pokemon should have stats, properties, and functions.
  106. let myPokemon = {
  107. name : "Bulbasaur",
  108. level : 3,
  109. health : 100,
  110. attack : 50,
  111. tackle : function(){
  112. console.log(`${this.name} tackled another pokemon.`);
  113. console.log(`targetPokemon's health is now reduced.`);
  114. },
  115. faint : function(){
  116. console.log(`${this.name} fainted.`);
  117. },
  118. };
  119. myPokemon.tackle();
  120. myPokemon.faint();
  121. // Creating object with an object constructor
  122. function Pokemon(name, lvl, hp){
  123. // Properties
  124. this.name = name;
  125. this.lvl = lvl;
  126. this.health = hp * 2;
  127. this.attack = lvl;
  128. // Methods
  129. this.tackle = function(target){
  130. console.log(`${this.name} tackled ${target.name}`);
  131. console.log(`targetPokemon's health is now reduced.`);
  132. console.log(target.name);
  133. console.log(this.name);
  134. };
  135. this.faint = function(){
  136. console.log(`${this.name} fainted.`)
  137. }
  138. };
  139. // Create new instances of the Pokemon object each with their unique properties.
  140. let pikachu = new Pokemon("Pikachu", 3, 50);
  141. let ratata = new Pokemon("Ratata", 5, 25);
  142. // Providing the "ratata" object as an argument to "pikachu" tackle method will create interaction between two objects.
  143. pikachu.tackle(ratata);
  144. ratata.tackle(pikachu);