Student Lab Workbook: A Journey into JavaScript Objects
Introduction
Welcome to this journey into JavaScript and its Object-Oriented features.
This workbook will guide you through the fundamentals of JavaScript, leading up to an enlightening exploration into Objects.
An OBJECT is : Data Fields PLUS METHODS
An OBJECT is box. This box is an object is an in-memory data structure which does tight binding of data and methods]. This tight binding give rise to the ‘this’ keyword.
We will investigate the nature of JavaScript Objects as we learn about the Debugger in JavaScript.
This lab worksheet provides hands-on exercises and examples to solidify your understanding and skills.
Section 1: JavaScript Fundamentals
Exercise 1: Hello, World!
Let's start by writing a simple JavaScript program.
Open your text editor and create a new file named hello.js. Write the following code in the file: console.log('Hello, World!');
Save the file and run it using Node.js by typing node hello.js in your terminal. Confirm that it prints Hello, World! to the console. Exercise 2: Variables and Constants
Understand the use of variables and constants.
Declare a variable name and assign your name to it. Declare a constant birthYear and assign your birth year to it.
let name = 'Your Name';
const birthYear = 1990;
console.log(name, birthYear);
Section 2: Control Flow
Exercise 3: Conditional Statements
Practice using if, else if, and else statements.
Write a program that checks if a number is positive, negative, or zero.
let number = prompt("Enter a number: ");
if (number > 0) {
console.log("The number is positive.");
} else if (number < 0) {
console.log("The number is negative.");
} else {
console.log("The number is zero.");
}
Exercise 4: Loops
Get comfortable with loops by writing a program that counts from 1 to 10.
// Let's make a 10x10 multiplication table
for (let i = 1; i <= 10; i++) {
for (let j = 1; j<= 10; j++){
console.log( i + " * " + j + " is " + i*j )
}
}
Section 3: Functions
Reference this worksheet for more fun with Functions:
Exercise 5: Creating Functions
Create a function that takes two numbers and returns their sum.
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // Outputs: 7
Section 4: Arrays
Exercise 6: Working with Arrays
Work with arrays and understand their features.
Create an array of your favorite books. Use a loop to print each book.
let books = ['Book1', 'Book2', 'Book3'];
for (let i = 0; i < books.length; i++) {
console.log(books[i]);
}
Section 5: Introduction to Objects
Exercise 7: Creating and Accessing Objects
Learn to create and access object properties.
Create an object person with properties firstName, lastName, and age. Access and print each property.
let person = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
console.log(person.firstName); // Outputs: John
console.log(person.lastName); // Outputs: Doe
console.log(person.age); // Outputs: 30
More Fun with JavaScript Objects:
Write a Javascript program to show that object references to functions can be called with the parameters to those functions
Here is a simple example demonstrating that object references to functions can indeed be called with the parameters to those functions in JavaScript:
// Defining an object with a method
const mathOperations = {
add: function(a, b) {
return a + b;
},
multiply: function(a, b) {
return a * b;
}
};
// Reference to the 'add' method of the 'mathOperations' object
const addReference = mathOperations.add;
// Calling the referenced function with parameters
const sum = addReference(2, 3);
console.log(sum); // Output: 5
// Reference to the 'multiply' method of the 'mathOperations' object
const multiplyReference = mathOperations.multiply;
// Calling the referenced function with parameters
const product = multiplyReference(4, 5);
console.log(product); // Output: 20
In this example:
An object mathOperations is defined with two methods: add and multiply. References to these methods (addReference and multiplyReference) are stored in separate variables. These references are then called with parameters ((2, 3) and (4, 5) respectively), and the results are logged to the console (5 and 20 respectively). This shows that you can store references to object methods in variables and call them later with parameters, just like you would with any other function reference in JavaScript.
image.jpeg failed to upload
Here is a Javascript program with an array - which demonstrates that JavaScript OBJECTS ARE ARRAY, with functions and data variables as members.
We can access those members with dot accessor notation, using the function as an object to show to students that an Object in JavaScript IS a function.
In JavaScript, functions are first-class objects, meaning they can have properties and can be assigned to variables, passed as arguments, and returned from other functions.
Below is a simple example that you can use to illustrate to your students that a function in JavaScript is indeed an object and can hold other functions and variables as properties.
The example uses an array to hold functions and data variables as members, and accesses those members using dot notation.
// Creating a function and adding properties to it
function greeting() {
console.log('Hello!');
}
// Adding other functions as properties to the greeting function
greeting.sayGoodbye = function() {
console.log('Goodbye!');
};
// Adding a data variable as a property to the greeting function
greeting.courseName = 'JavaScript Fundamentals';
// Creating an array that contains functions and data variables
const array = [greeting, greeting.sayGoodbye, greeting.courseName];
// Accessing and invoking the function stored in the array
array[0](); // Output: Hello!
// Accessing and invoking the function stored as a property in the greeting function object
array[1](); // Output: Goodbye!