JavaScript Objects Part 2


How do you make use of objects in JavaScript? The previous post Part 1 described the basics of JavaScript objects. Have a look. Suppose your objects are a bunch of people. Each person has a list of properties that describe them. You need to go through each person and run some logic that displays a list of people that are over the age of 50. For this example a list of names will be displayed.

First Name Last Name Age Gender
Billy Thorton 56 male
Sally Smith 32 female
Brittany Johnson 71 female

The code below shows how to put the above data into JavaScript objects and also shows the functions we could use.

var person1 = {
	firstname: "Billy",
	lastname: "Thorton",
	age: 56,
	gender: "male"
}
var person2 = {
	firstname: "Sally",
	lastname: "Smith",
	age: 32,
	gender: "female"
}
var person3 = {
	firstname: "Brittany",
	lastname: "Johnson",
	age: 71,
	gender: "female"
}
function prequalify(person) {
	if (person.age >= 50) {
		return true;
	}
	else {
		return false;
	}
}
function processPeople() {
	var worthALook (prequalify(person1));
	if (worthALook) { document.write(person.firstname); }
	var worthALook (prequalify(person2));
	if (worthALook) { document.write(person.firstname); }
	var worthALook (prequalify(person3));
	if (worthALook) { document.write(person.firstname); }
}

Leave a comment

Your email address will not be published. Required fields are marked *