JSFiddle - React, Tailwind, and code Playground
by DustyWhite
HTML
<div>
Private Object Properties.
<p>Open JavaScript Console to see results.</p>
</div>
JavaScript
"use strict";
// Array to hold Items
var items = [];
// Create Constructor Function for an Item Object
function Item(name, quantity, comment) {
var name = name;
var quantity = quantity;
var comment = comment;
this.getItemInfomration = function(){
return name + ". Qty. " + quantity + ". " + 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 private
//console.log(item1.name);
// But, on the surface, it looks like we can change the Object's name Property outside of the Object.
//console.log('item1 before item name change:');
//console.log(item1);
item1.name = 'Butter';
//console.log(item1.name);
//console.log('item1 after item name change:');
//console.log(item1);
// Note that this added a new PUBLIC property to the Object!
// It did not change the value of the PRIVATE name property.
//console.log(item1.getItemInfomration());