Object Literal

by Scott Currell

HTML

<script src="https://cdn.jsdelivr.net/gh/eu81273/jsfiddle-console/console.js"></script>

JavaScript

//Object Literal declaring a property and method
var skillet = {
  //public property
  ingredient: "Bacon Strips",

  //public method
  fry: function() {
    console.log("Frying " + this.ingredient);
  }
};

console.log(skillet.ingredient); //Bacon Strips

skillet.fry(); //Frying Bacon Strips

//Adding a public property to an Object Literal
skillet.quantity = "12";
console.log(skillet.quantity); //12

//Adding a public method to an Object Literal
skillet.toString = function() {
  console.log(this.quantity + " " +
    this.ingredient);
};

skillet.toString(); //12 Bacon Strips