[javascript] Objects

Viewer

  1.  // ex1
  2.  var rectangle = {
  3.   height: 10,
  4.   width: 5,
  5.   xCoordinate: 3,
  6.   yCoordinate: 5
  7.  }
  8.  
  9.  for(var prop in rectangle){
  10.   document.write(prop + ": " + rectangle[prop] + "<br>")
  11.  }
  12.  
  13.  
  14.  // ex2
  15.  var employee = {
  16.   firstName: "Or",
  17.   lastName: "Hasson",
  18.   salary: 100300,
  19.   email: "[email protected]",
  20.   phone:"0542121212"
  21.  }
  22.  
  23.  for(var prop in employee){
  24.   document.write(prop + ": " + employee[prop] + "<br>")
  25.  }
  26.  
  27.  //ex3
  28.  var clothes = {
  29.   brand: "Nike",
  30.   size: "M",
  31.   color: "Red",
  32.   price: 10
  33.  }
  34.  
  35.  for(var prop in clothes){
  36.   document.write(prop + ": " +clothes[prop] + "<br>");
  37.  }
  38.  
  39.  var clothesInput = {
  40.   brand: prompt("Insert the brand:"),
  41.   size: prompt("Insert the size: "),
  42.   color: prompt("Insert the color: "),
  43.   price: +prompt("Insert the price: ")
  44.  }
  45.  
  46.  for(var prop in clothesInput){
  47.   document.write(prop + ": " +clothesInput[prop] + "<br>");
  48.  }
  49.  
  50.  
  51. //ex4
  52.  var customer = {
  53.   firstName: "Or",
  54.   lastName: "Hasson",
  55.   email:"[email protected]",
  56.   phone: "054-2226222",
  57.   creditCard: {
  58.     type: "Visa",
  59.    number: "5428-2343-3244-3443",
  60.    expiration: "12/2029",
  61.    cvc: "789"
  62.   }
  63.  }
  64.  
  65.  for(var prop in customer){
  66.    if(prop === "creditCard"){
  67.     for(var innerProp in customer.creditCard){
  68.      document.write(prop + " ->" + innerProp + ": " + customer.creditCard[innerProp] +"<br>");
  69.     }
  70.    }
  71.    else {
  72.     document.write(prop + ": " + customer[prop] + "<br>");
  73.    }
  74.  }
  75.  
  76.  

Editor

You can edit this paste and save as new:


File Description
  • Objects
  • Paste Code
  • 26 Nov-2020
  • 1.42 Kb
You can Share it: