JSON Examples

8- Multiple objects

Output

JS

// this function will initialize new 'Person' objects
function Person(first, middle, last, age) {
    this.firstName   = first;
    this.midName     = middle;
    this.lastName    = last;
    this.age         = age;
}

// this adds the getName() method to the 'Person' object
Person.prototype.getName = function() {
    return this.firstName + ' ' + this.lastName;
}

HTML

<script>
    let info = document.getElementById('info'),
        br   = '<br>';
                
    var jake     = new Person('Jake', 'Woodrow', 'Long', '44');
    var alice    = new Person('Alice', 'Elizabeth', 'Thrift', '64');
    var fred     = new Person('Fred', 'Floober', 'Stonewall', '21');

    info.innerHTML = '1. ' + jake.getName() + br +
                     '2. ' + alice.getName() + br +
                     '3. ' + fred.getName();
</script>