Title: Objects Lab
Class: SEI
Contributors: GA Instructional Team
Topics: JavaScript objects
List and describe each individual piece of syntax that we use to construct an object. Don't leave anything out! The list is finite.
Example:
// {} curly braces define the object.- Create an empty object called
me. - Assign it properties for name, age, and email with corresponding values. Afterwards, logging the object should give the following output:
{name: "Kristyn", age: 98, email: "kristyn@foo.bar"}or
{
name: "Kristyn",
age: 98,
email: "kristyn@foo.bar"
}- Using dot notation, log the name property in your object.
- Using bracket notation, update the value of age to be 1000 years old.
- Using dot notation,
console.log()age to verify that it has been updated. - Add a property to this object called: "place of residence" and give it a value of your hometown. Note that the key has spaces, therefore you cannot use dot notation.
- Print the value of "place of residence"
const monster = {
name: "Slimer",
color: "greenish",
type: "plasm or ghost or something"
}Given the slimer object, do the following:
console.log()thename.- change the
typeto 'creature'. - Add a property to the object called
age, and set its value to 6. console.log()the object to make suretypeandageare what you want them to be.- give the slimer a method to introduce himself, interpolating some of his properties. call the method.
Let's say you want to make an adventure where an adventurer and an ogre fight each other.
Spend a few minutes reading this question a couple times and thinking and pseudocoding before you actually write the code.
—> Write a very small program that will simulate a battle between your adventurer and an ogre.
- The battle should play out automatically.
- The ogre and adventurer will take turns attacking each other, and statistics will be shown after each attack.
- The ogre's attacks should have random damage value, but the adventurer should always attack with the same value.
- Whenever someone's hitpoints go below zero, the other person wins the battle.
Create objects and have them interact. Remember, you are modeling things from real life. So just like the characters you are modeling, your objects will have properties (qualities) and methods (things they can do).
- how would you define your
adventurer? Your adventurer will want a name and hitpoints. What else would your adventurer need? - how would you define an
ogre? Your ogre will want hitpoints, right? (Right.) - How could you implement the "attack" functionalities? Should you do this by adding methods to the objects? Should those methods take parameters?
- Use
console.log()to show each attack, how many hitpoints the person being attacked loses, the updated stats for the ogre and the adventurer. At the end log the winner. - You can use a loop to make the game play out. (What kind of loop? How and when will it stop?)
- Would it be helpful to create another object to control and keep track of other data that isn't specifically about the ogre or the adventurer? Like a
gameobject maybe?
Doing this efficiently requires planning. If you just started coding immediately without thinking through what you're trying to do and planning, then you're doing it wrong. Timewise, programming is 75-90% planning, and 10-25% actually typing code.
-
Define an object called
cat1that contains the following properties:- name
- breed
- age (a number)
-
console.log the cat's age
-
console.log the cat's breed
- Define an object called
cat2that also contains the properties:- name
- breed
- age (a number)
The cats are multiplying!
Write a function combineCats that has two parameters mama, and papa. The function will take two arguments -- each a cat object.
- Pass
cat1andcat2as arguments to thecombineCatsfunction. The function should console.log them.
Example:
combineCats(cat1, cat2){ name: "Joe", age: 19, breed: "Mog" }
{ name: "Jam", age: 45, breed: "Siamese" }
This is to demonstrate that functions can take objects as arguments
You could also invoke the combineCats function by writing the objects straight into the parentheses:
combineCats({ name: "Craig", age: 20, breed: "unknown" }, { name: "Linda", age: 20, breed: "undefined" });- Make it so the
combineCatsfunction will return a combination of the two incoming cats- The result should be an object wherein the
- name is a concatenation of the parents' names
- the age is 1
- the breed is each of the parents' breeds with a hyphen in between
- The result should be an object wherein the
Example:
console.log(combineCats(cat1, cat2));Result:
This is to demonstrate that a function can return an object
If combineCats returns an object, and if combineCats takes objects as arguments, then it stands to reason that:
catCombinator can use itself as its own argument.
Take a second to stew on that . . .
What is the result of:
console.log(combineCats(combineCats(cat1, cat2), combineCats(cat1, cat2)));Whoa . . .
The above console.log is two levels deep of combineCats.
- Write a console.log that is three levels deep of combineCats. combineCats should have two arguments, each which are combineCats, each which have two arguments, each which are combineCats.
Your output should look something like:

