/index.js

https://gitlab.com/zuitt.projects-hill/s18 · JavaScript · 185 lines · 90 code · 45 blank · 50 comment · 0 complexity · 097cb20c3a538b1611fe67119cec1cc2 MD5 · raw file

  1. //alert("Itsa me .. Mario!")
  2. // A variable with multiple values
  3. //a: array
  4. let cartoons = ["Elmo", "Mickey", "Daisy", "Barbie"]
  5. //Javascript Objects
  6. // Similar to an array, it also contains multiple values
  7. // Unlike an array that uses index, objects use properties
  8. // with objects, we can easily give labels to each value
  9. //Structure or Syntax
  10. /*
  11. let objectName = {
  12. key: value,
  13. key: (function),
  14. key: object{
  15. key: value
  16. },
  17. key: [array]
  18. }
  19. */
  20. let cellphone = {
  21. name: "Nokia 3210",
  22. manufactureDate: 1999
  23. };
  24. console.log(cellphone);
  25. console.log(typeof cellphone);
  26. // Creating objects using a constructor function
  27. /*
  28. Syntax:
  29. function objectName(keyA, keyB){
  30. this.keyA = keyA;
  31. this.keyB = keyB;
  32. };
  33. */
  34. function Laptop(name, manufactureDate){
  35. this.name = name;
  36. this.manufactureDate = manufactureDate;
  37. };
  38. let laptop = new Laptop("Lenovo", 2008);
  39. console.log(laptop);
  40. //will create new object
  41. //new operator creates an instance of an object
  42. let laptop1 = Laptop("Asus", 2010);
  43. console.log(laptop1);
  44. //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. //access the property of an object inside of an array
  53. console.log(machines[0].name);
  54. // lenovo
  55. //another way of accessing properties in an array
  56. console.log(machines[0]["manufactureDate"]);
  57. // 2008
  58. // Initializing/ Adding/ Deleting/ Reassigning Object Properties
  59. let car = {};
  60. //initializing/ adding object properties usinf 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 = "Blue";
  72. car.location = "Cebu";
  73. console.log(car);
  74. //reassigning object properties
  75. car.manufactureDate = 1986
  76. console.log(car);
  77. //Object Methods
  78. // a method in an obeject 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 methoids are functions related to a specific object.
  80. // methods are defined based on what an object is capable of doing and 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: "Joen",
  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 personal email is ${this.emails[0]}.`)
  98. }
  99. };
  100. friend.introduce();
  101. let myPokemon = {
  102. name: "Bulbasaur",
  103. level: 3,
  104. health: 100,
  105. attack: 50,
  106. tackle: function(){
  107. console.log(`${this.name} tackled another pokemon.`)
  108. console.log(`targetPokemon's health is now reduced.`)
  109. },
  110. faint: function(){
  111. console.log(`${this.name} fainted.`)
  112. }
  113. };
  114. myPokemon.tackle();
  115. myPokemon.faint();
  116. // creating object with an object constructor
  117. function Pokemon(name, lvl, hp){
  118. // properties
  119. this.name = name;
  120. this.lvl = lvl;
  121. this.health = hp * 2;
  122. this.attack = lvl;
  123. //methods
  124. this.tackle = function(target){
  125. console.log(`${this.name} tackled ${target.name}`);
  126. console.log(`targetPokemon's health is now reduced`);
  127. console.log(target.name);
  128. console.log(this.name);
  129. };
  130. this.faint = function(){
  131. console.log(`${this.name} fainted.`)
  132. }
  133. };
  134. // create new instances of the Pokemon object each with their unique properties
  135. let pikachu = new Pokemon("Pikachu", 3, 50);
  136. let ratata = new Pokemon("Ratata", 5, 25);
  137. //providing the "ratata" object as an arguemtn to "pikachu" tackle method will create interaction between the two objects
  138. pikachu.tackle(ratata);
  139. ratata.tackle(pikachu);