S-3: Fiddle 15

Object Constructor Functions.

by Jordan Marechal

HTML

<h1>
  CSCI S-3
  <br />Introduction to Web Programming Using JavaScript
</h1>
<h2>
  Harvard Summer School
  <br />2017
</h2>
<h3>TF: Rob Frenette</h3>
<div>
  Object Constructor Functions.
  <br />Open JavaScript Console to see results.
</div>

JavaScript

"use strict";

console.clear();

// Array to hold Items
var items = [];

// Create Constructor Function for an Item Object
function Item(name, quantity, comment) {
	this.name = name;
	this.quantity = quantity;
	this.comment = comment;

  this.getItemInfomration = function(){
	  return this.name + ". Qty. " + this.quantity + ". " + this.comment;
	};
}

// Create some Item Objects and put them in the Items Array
var item1 = new Item("Milk", 1, "Low Fat");
items.push(item1);

var item2 = new Item("Eggs", 12, "Large");
items.push(item2);

var item3 = new Item("Bread", 1, "Whole Grain");
items.push(item3);

// Display Items in Console
items.forEach(function(item) {
    //console.log(item.getItemInfomration());
});

// Note that the properties of these Items are public
//console.log(item1.name);
// So, we can change the value outside of the Object. 
item1.name = 'Butter'; 
//console.log(item1.name);