Foundations Updated 2026-09 View as Markdown

Object Oriented JavaScript

JavaScript uses functions as classes to create objects using the new keyword.

JavaScript uses functions as classes to create objects using the new keyword. Here is an example:

javascript
function Person(firstName, lastName) {
    // construct the object using the arguments
    this.firstName = firstName;
    this.lastName = lastName;

    // a method which returns the full name
    this.fullName = function() {
        return this.firstName + " " + this.lastName;
    }
}

let myPerson = new Person("John", "Smith");
console.log(myPerson.fullName());            // outputs "John Smith"

Creating an object using the new keyword is the same as writing the following code:

javascript
let myPerson = {
    firstName : "John",
    lastName : "Smith",
    fullName : function()
    {
        return this.firstName + " " + this.lastName;
    }
}

The difference between the two methods of creating objects is that the first method uses a class to define the object and then the new keyword to instantiate it, and the second method immediately creates an instance of the object.

Exercise

Try it

ready

Expected output

Jack, 25 years old
Jill, 24 years old

Get the JavaScript agent pack

A battle-tested AGENTS.md, the review checklist, and the failure-mode cheat sheet for JavaScript. One email, then occasional updates when the tooling shifts. No course pitch.

Unsubscribe in one click. We never sell the list. Or just take the AGENTS.md now — no email needed.