Lab 1
Classless Objects
Overview and Objectives
In this lab, learners explore the fundamentals of JavaScript objects by creating, modifying, and enumerating object literals and understanding prototype-based inheritance. They practice accessing properties using dot and bracket notation, checking property existence, and adding methods to objects. By the end of this lab, learners will be able to:
- Create and manipulate JavaScript objects using literal syntax.
- Access and modify object properties with dot notation and bracket notation.
- Test for property existence and enumerate properties of an object.
- Understand object references and how assigning objects affects them.
- Define methods (functions) as object properties.
- Explore prototype-based object creation without using classes.
Tasks and Instructions
Task 1: Creating an Object Literal
- Define an object literal named
person
with at least three properties: for example,name
,age
, andhobby
. Choose appropriate values for these properties (e.g., name as a string, age as a number, hobby as a string or array). Use the syntax{ key: value, ... }
to create the object.- Hint: For example:
const person = { name: "Alice", age: 25, hobby: "painting" };
. - After creation, use
console.log(person)
to print the entire object and verify its structure.
- Hint: For example:
Task 2: Accessing and Modifying Properties
- Practice accessing properties using both dot notation and bracket notation.
- Using dot notation, log the person’s name (e.g.,
console.log(person.name)
). Using bracket notation, log the hobby (e.g.,console.log(person["hobby"])
). - Add a new property to the object using bracket notation. For instance, add a property
email
toperson
(e.g.,person["email"] = "alice@example.com"
). Then, output the updated object to confirm the new property was added.
- Using dot notation, log the person’s name (e.g.,
Task 3: Property Existence Check
- Check whether certain properties exist in the
person
object.- Use the
in
operator to test for a property that exists (e.g.,"name" in person
) and one that doesn’t exist (e.g.,"address" in person
). Log the results (true/false) to the console. - Also use the
.hasOwnProperty()
method to check for one of the existing properties (e.g.,person.hasOwnProperty("age")
). This will confirm if the property is a direct member of the object. Log the result.
- Use the
Task 4: Enumerating Properties
- Use a
for...in
loop to iterate over all enumerable properties of theperson
object. Inside the loop, print each property name and its value in the format"propertyName: value"
.- Hint:
for (let key in person) { console.log(key + ": " + person[key]); }
. - Verify that all expected properties (including the ones added) are listed. Note that inherited properties (if any) would also appear; in a plain object literal with no prototype changes, you should mostly see the properties you defined.
- Hint:
Task 5: Object References
- Create a new variable
personRef
and assign it to theperson
object (e.g.,const personRef = person;
). Then, usepersonRef
to modify one of the properties (for example, change theage
property). After modification, useconsole.log(person.age)
to observe if the original object’s property has changed.- This step demonstrates that objects are assigned by reference, not copied. Both
person
andpersonRef
should reflect the change because they point to the same object in memory.
- This step demonstrates that objects are assigned by reference, not copied. Both
Task 6: Adding a Method
- Add a function property (method) to the
person
object. For example, add a methodgreet
that prints a greeting using the person’s name. You can define it as:person.greet = function() { console.log("Hello, my name is " + this.name + "!"); };
- Call
person.greet()
to ensure it outputs the expected greeting. - Try accessing the method with bracket notation as well (e.g.,
person["greet"]()
).
- Call
Task 7: Prototype-based Creation
- Explore another way to create an object using a prototype. First, create a base object
animal
with a couple of properties (e.g.,species
andcanFly
). For example:Now useconst animal = { species: "generic", canFly: false };
Object.create(animal)
to create a new objectbird
that usesanimal
as its prototype. Add a new property tobird
such asspecies = "bird"
and maybecanFly = true
.- After creation, log
bird.species
andbird.canFly
. Also logbird.hasOwnProperty("canFly")
to check ifcanFly
is an own property ofbird
or inherited fromanimal
. - Modify a property on the prototype (e.g., set
animal.canFly = false
or changeanimal.species
) and then log the corresponding property onbird
to observe how the prototype change reflects in the derived object. This demonstrates JavaScript’s prototype-based inheritance without using classes.
- After creation, log
Task 8: Using Object.defineProperty
- As an additional exploration, use
Object.defineProperty
to add a new property toperson
(or modify an existing one) with specific descriptors (like making it read-only or non-enumerable). For example, define a propertynickname
onperson
that cannot be changed once set. (This optional step is for you to explore property configuration; it is not required to complete the lab but builds deeper understanding.)
Submission Criteria
Via a GitHub repository, submit the following via Canvas:
- Submit your JavaScript source file (e.g.,
lab1.js
) containing the code for the above tasks. Ensure that each step’s code is executed in order (you can keep all code in one file, using comments to separate tasks). - Your submission should include demonstration of results (for example, console.log outputs) for each task. You can either include the console output as comments in the code or submit a screenshot of the console showing your code’s output for verification.
- The code should run without errors in a Node.js environment or a web browser console. Make sure all object manipulations and outputs work as described.
Grading Rubric
Criteria | Points |
---|---|
Correctly created an object literal with the required properties. | 5 points |
Used dot notation and bracket notation to access and modify properties appropriately. | 5 points |
Successfully tested property existence using in and/or .hasOwnProperty and logged results. | 5 points |
Enumerated object properties using a for...in loop and output all key-value pairs. | 5 points |
Demonstrated object reference behavior by modifying a copied reference and explained/observed the effect. | 5 points |
Added a method to the object and invoked it correctly (method produces the expected output). | 5 points |
Created a new object using a prototype (Object.create ) and showed inheritance of properties (including using hasOwnProperty to distinguish own vs inherited). | 8 points |
Code clarity, organization, and adherence to instructions (meaningful variable names, commented outputs, no runtime errors). | 2 points |
Total Points: 40 points
Note
Partial credit will be awarded for completing some, but not all, of the tasks. Each step is assessed independently per the rubric, so even if one part of your code is not working, you can earn points for the successful parts.