CS133JS Beginning Programming: JavaScript

Intro to Objects

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects and Object Constructors
3. Conditional Statements: if and switch8. Web Site I/O
4. Repetition: while, do while, and for9. Review
5. Midterm Check-Point10. Final

Table of Contents

Introduction

Q and A

 

Review of Array Methods

These examples use the array declared and initialized below:

 

length

indexOf()

splice

concat

Copy an array.

pop

shift

push

unshift

 


Overview of objects

Objects in JS are like objects in the real world. They have properties--for example a book has: title, author, publication date, genre, etc. They can also have methods--for example a book could have a countWords method that counts all the words in the book.

Objects can contain:

Creating Objects

There are two ways to create an object:

 

Properties and Methods

Think of a properties as nouns, and methods as verbs.

Properties

Each property consists of a key : value pair. In the example above, name is a key and "Fili" is a value.

Methods

A method is essentially a function. It is also the value of an object property. It is a task that an object can perform. In the example above, greet is a key, and is the name of the method. The function is a value.

Accessing Properties and Methods

Encapsulation

The scope of properties and methods declared inside an object is limited to that object. The rule about local scope being delineated by the curly braces applies here. There is a special word for the scope of properties and methods inside an object. We say they are encapsulated in the object.

Accessing an Object Property

Here are two ways to access an object’s properties.

Bracket notation must be used if an object’s property name is a number.

What do objects with numeric properties look like?

The this keyword

Inside a method, the this keyword references the object that contains the method so you can access an object property.

 

Calling a Method on an Object

You call a method just as you would call a regular function, but you do it through the object.

Adding and Removing Properties and Methods

Adding Properties to an Object

Add new properties to an object with the assignment operator, =

Adding Methods to an Object

Adding a method to an object is similar to adding a property, but in place of the property value you provide a function definition.

Removing Properties from an Object

utilize the delete keyword. Evaluates to true if successfull.

Using Loops with Object Properties

for...in LoopJust for Objects

The JavaScript language has a special, predefined loop, for...in, that is specifically designed for iterating over the properties of an object. This loop only iterates over property keys (aka names). Note that you need to use bracket notation.

All the Other Loops

Get an Array with Object.keys() or Object.values()

JavaScript objects have a pre-defined method for getting an array of property keys, the Object.keys() method. This makes it easy to use all the other loops with object properties.

You can also get an array of all the values in an object like this:

 

####for...of LoopJust for Arrays

The for...of loop is specifically designed to operate on arrays.

for Loop

The for loop has a built-in loop counter that works great for indexing an array.

while and do...while Loops

We can use these loops on arrays of object properties too, but they aren't as convenient to use as the three types of for loops.

Mutability of Objects

Objects declared with const are mutable, that means they can have their properties changed or removed and can have new properties added. But the binding is immutable, the object variable can't be assigned to a new object.

 

References

JavaScript Object Basics - MDN Guide

Understanding Objects in JavaScript - Tania Rascia, 2021, Digital Ocean

"Data Structures: Objects and Arrays", Ch. 4 in Eloquent JavaScript, 4th Ed., Marijn Haverbeke, 2024, No Starch Press.

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird written in 2020 and revised in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.