JSFiddle - React, Tailwind, and code Playground
by DustyWhite
HTML
<div>
Object Constructor Functions.
<p>Open JavaScript Console to see results.</p>
</div>
JavaScript
"use strict";
// Array to hold Items
var items = [];
console.log(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);
//console.log(items);
//console.log(typeof items[0]);
//console.log(items[0].constructor.name);
var item2 = new Item("Eggs", 12, "Large");
//items.push(item2);
//console.log(items);
var item3 = new Item("Bread", 1, "Whole Grain");
//items.push(item3);
//console.log(items);
// 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);