Title: OOP Westworld Lab
Type: Lab
Duration: 1 hr
Creator: Thom Page
Modified by: Kristyn Bryan
Topics: Object methods, Constructors
OOP Westworld Lab
Setup
- In
student_labs
folder, make a new fileoop_westworld.js
. - Give
oop_woestworld.js
a console.log. - Run it in node
node oop_westworld.js
. Verify the console log appears. - Work inside the
oop_westworld.js
file for this lab.
WESTWORLD
From Wikipedia:
[Westworld] takes place in fictional Westworld, a technologically advanced, Western-themed amusement park populated completely by synthetic androids dubbed "hosts".
Make your own Westworld hosts
You are going to make some androids for the Westworld park!
Create a host
Make an object called host
that has the following properties:
- name
- occupation
Give your host
object a method called saySpecs
.
The saySpecs
method should output a message from the host listing the host's specifications -- the host's name and occupation:
=> "My name is Roget. My occupation is creator of Roget's Thesaurus."
Create some basic hosts
Now let's make a class so that we can easily make many hosts.
Make a class called BasicHost
that takes parameters for name and occupation.
class BasicHost {
constructor(name, occupation) {
//stuff here
}
}
Make it so the BasicHost
function will spit out a host object.
var host1 = new BasicHost("Roget", "creator of Roget's Thesaurus");
console.log(host1);
Output:
Make another instance with the BasicHost called host2
.
Augment your basic hosts
Make it so that your BasicHost
class, when run, will also add a method to your host objects. Give your class a saySpecs
method that will log all of the specs of the instance.
Create a few more basic host objects with your constructor.
Invoke the saySpecs
function on those hosts.
Populate the world with hosts
How many hosts can we make???? We are going to populate an array with host objects using a for loop.
We will need a pool of names to draw from. Make an array called names
, and add in a few names. Here's one if you want to use it:
taken from http://listofrandomnames.com/
const names = [
"Laila", "Jack", "Harley", "Hertha", "Darren", "Jolene",
"Loura", "Lona", "Davida", "Reena", "Leland", "Ta", "Jen",
"Linn", "Roslyn", "Margorie", "Rafaela", "Romona", "Shanel", "Stan"
];
Make an array called occupations
and add in a few occupations. Here's one if you want to use it:
const occupations = [
"Clerical assistant", "Leaflet distributor", "Landowner",
"Special constable", "Anaesthetist", "Park-keeper", "Butler",
"Choreographer", "Blacksmith", "Chef", "Legal secretary",
"Song writer", "Librarian", "Landscape gardener"
];
Make an empty array called hostArray
. This is where we will store our host objects.
Write a for loop that will will run 100 times. Inside the for loop, push a new BasicHost into the array:
hostArray.push(new BasicHost());
After the loop, console.log the hostArray.
We have 100 hosts, but they have no values for their attributes:
FIGURE IT OUT
Make it so that when a new BasicHost is pushed into the array, that host will be assigned a random name and random occupation from the names and occupations arrays.
If it works, you should have an array of objects of a variety of different hosts.
hostArray:
Make a host speak.
hostArray[55].saySpecs()
Bonus
Make your hosts more interesting by giving them random numerical values from 1-20 for personality traits (how ever many you would like):
- Empathy
- Loyalty
- Aggression
- Curiosity
- Bulk Apperception
Etc.
- This way, everything is done with methods
- Other developers can quickly scan the class definition to determine what you'd like them to be able to do with the class
Objects interacting with other objects
We can pass an object to another object to have them interact
class Person {
constructor(name, age, eyes, hair, lovesCats = false, lovesDogs) {
this.legs = 2;
this.arms = 2;
this.name = name;
this.age = age;
this.eyes = eyes;
this.hair = hair;
this.lovesCats = lovesCats;
this.lovesDogs = lovesDogs || false;
}
greet(otherPerson) {
console.log('hi ' + otherPerson + '!');
}
classyGreeting(otherClassyPerson) {
console.log('Greetings ' + otherClassyPerson.name + '!');
}
setHair(hairColor) {
this.hair = hairColor;
}
walk() {
console.log('I hate when my Segway is in the shop.');
}
}
const me = new Person(
'Karolin',
41,
'green',
'copper dark ash blond',
true,
true
);
const you = new Person('Matt', 36, 'hazel', 'brown');
me.classyGreeting(you);
you.classyGreeting(me);