<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);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.